模型不学习 [英] Model is not learning

查看:158
本文介绍了模型不学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在来自我的网络摄像头的图像上训练Tensor-flow js模型.基本上,我正在尝试重新创建pac-man张量流游戏.该模型无法收敛,经过训练后几乎没有用.我感觉到它是如何准备数据的.

I am trying to train a Tensor-flow js model on images coming in from my web cam. Basically I'm trying to recreate the pac-man tensor-flow game. The model isn't converging and is pretty much useless after training. I have a feeling its how I'm prepping the data.

从画布上抓取图像

function takePhoto(label) {
  let canv = document.getElementById("canv")
  let cont = canv.getContext("2d")
  cont.drawImage(video, 0, 0, width, height)

  let data = tf.browser.fromPixels(canv, 3)
  data.toFloat().div(tf.scalar(127)).sub(tf.scalar(1))
  return data
}

function addExample(label){
      let data = takePhoto()

      addData(train_data => train_data.concat(data))
      addLabel(train_labels => train_labels.concat(labels[label]))
    }

训练功能

export async function train_model(image,label){
    let d = tf.stack(image)

    let l = tf.oneHot(tf.tensor1d(label).toInt(),4)

    let data = await model.fit(d,l,{epochs:10,batchSize:label[0].length,callbacks:{
        onBatchEnd: async  (batch, logs) =>{ 
            console.log(logs.loss.toFixed(5))
        }
    }})
    return data
}

型号

export function buildModel(){
    model = tf.sequential({layers:[ 
        tf.layers.conv2d({inputShape:[width,height,3],
                            kernelSize:3,
                            filters:5, 
                            activation :"relu"}),
        tf.layers.flatten(),
        tf.layers.dense({units:128, activation:"relu",useBias:true}),
        tf.layers.dense({units:32, activation:"relu"}),
        tf.layers.dense({units:4, activation:"softmax"})
    ]})
    model.compile({metrics:["accuracy"], loss:"categoricalCrossentropy", optimizer:"adam",learningRate:.00001})
    console.log(model.summary())
}

预测

export async function predict(img){

    let pred = await tf.tidy(() => {

        img = img.reshape([1,width,height, 3]);

        const output = model.predict(img);

        let predictions = Array.from(output.dataSync());
        return predictions
    })
    return pred
}

回调会显示损失,但它们不会收敛到任何东西,并且预测很遥远(随机)

The callback prints the losses but they do no converge to anything and the predictions are way off (random)

推荐答案

该模型是否使用了正确的模型?

一个人需要问的第一个问题是所使用的模型是否正确.该问题的模型使用了卷积层和密集层的混合体.但是该模型并没有真正遵循CNN的结构,而卷积层之后总是池化层.这是模型无法学习的原因吗?没必要...

The first question one needs to ask is if the model used is the right one. The model of the question uses a mixture of convolutional and dense layers. But the model does not really follow the structure of CNN whereas convolutional layers are always followed by pooling layers. Is it the reason why the model is not learning ? Not necessary ...

在分类问题中,对图像进行分类的方法有很多,各有其优缺点. FCNN不能达到良好的准确性,CNN却可以.但是训练CNN模型可能会耗费大量计算资源.这是转移学习发挥作用的地方.

In classification problems, there are different ways of classifying images each one with its pros and cons. FCNN does not achieve good accuracy, CNN does. But training a CNN model can be computation expensive. This is where transfer learning comes to play.

pacman示例使用转移学习.因此,如果您想复制该示例,请考虑遵循tfjs示例的github代码.这里的模型仅使用一个卷积层.关于如何编写转移学习模型.

The pacman example uses transfer learning. So if you want to replicate the example, consider following the github code of tfjs example. The model here uses only one convolutional layer. There are good tutorials on the official website of tensorflow as regard how to write CNN networks and transfer-learning models.

您使用了多少数据来训练模型?

深度学习模型通常需要大量数据.因此,除非模型看到很多带有标签的图像,否则其准确性非常低就不足为奇了.需要多少数据主要是艺术和设计问题,而不是科学问题.但是一般的经验法则是,有更多的数据,模型在预测中会更好.

Deep learning models in general needs a lot of data. So unless the model has seen a lot of images labelled, it won't be surprising if its accuracy is very low. How much data is needed is mostly a question of art and design than science. But a general rule of thumb, more there is data, better is the model in predicting.

调整模型

即使一个好的模型也需要调整其参数-时期数,批量大小,学习率,优化器,损失函数...更改这些参数并观察它们如何解释精度是实现良好精度的一个步骤.

Even a good model needs its parameter to be tuned - number of epochs, batchsize, learning rate, optimizer, loss function... Changing those parameters and observe how they account for the accuracy is a step in having good accuracy.

要指出的是,作为model.compile

这篇关于模型不学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆