简体   繁体   English

CNN 模型无法训练

[英]CNN Model unable to train

I'm trying to train a CNN model to give a prediction of format我正在尝试训练一个 CNN 模型来预测格式

array([ 0., 0., 0., 0., -1.], dtype=float32) . array([ 0., 0., 0., 0., -1.], dtype=float32)

My Training data looks like this :我的训练数据如下所示:

0        [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 0.0, 0....
1        [[0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 1.0, 0....
2        [[0.0, 0.0, 0.0, -1.0, -1.0], [0.0, 0.0, 0.0, ...
3        [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...
4        [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 0.0, 0....
                               ...                        
15484    [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 2.0, 1....
15485    [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...
15486    [[-1.0, -1.0, -1.0, -1.0, -1.0], [0.0, 2.0, 0....
15487    [[1.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0....
15488    [[-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, ...

With each row of shape (24,5) looking like this :每行形状 (24,5) 看起来像这样:

array([[-1., -1., -1., -1., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  3.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  2.,  0.,  0.],
       [ 0.,  3.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0., -1.],
       [ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 0.,  1.,  0.,  0., -1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.],
       [ 1.,  2.,  0.,  0., -1.]], dtype=float32)

The model I'm using looks like this :我使用的模型如下所示:

model = tf.keras.Sequential(layers=[
                tf.keras.layers.Conv2D(32,kernel_size = 3, activation=tf.nn.relu, input_shape=(28,28,1)),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(32,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(32,kernel_size = 5,strides=2,padding='same', activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),

                tf.keras.layers.Conv2D(64,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(64,kernel_size = 3, activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Conv2D(64,kernel_size = 3, strides=2, padding='same', activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Flatten(),
                tf.keras.layers.Dense(128,activation=tf.nn.relu),
                tf.keras.layers.BatchNormalization(),
                tf.keras.layers.Dense(1, activation=tf.nn.softmax)])

        model.compile(optimizer=tf.keras.optimizers.Adam(0.1), 
                      loss=tf.keras.losses.CategoricalCrossentropy(), 
                      metrics=tf.keras.metrics.Accuracy())

I'm new to the field and is currently getting the following error :我是该领域的新手,目前遇到以下错误:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Can someone tell me what I'm doing wrong here?有人可以告诉我我在这里做错了什么吗? I added a tf.convert_to_tensor() function on the training data but is still getting the same error.我在训练数据上添加了一个tf.convert_to_tensor()函数,但仍然出现相同的错误。

Adding .astype("float32") seems to not be working either.添加.astype("float32")似乎也不起作用。

The tf.convert_to_tensor() function should actually have done it. tf.convert_to_tensor()函数实际上应该已经完成​​了。 Your toy-example array converted likethis a_tensor = tf.convert_to_tensor(a, dtype=tf.int32) would be a valid input for the CNN, as would be an array of n such arrays (then with shape=(n, 24, 5) ).这样转换的玩具示例数组a_tensor = tf.convert_to_tensor(a, dtype=tf.int32)将是 CNN 的有效输入, n这样的数组的数组也是如此(然后shape=(n, 24, 5) )。 You could post a minimal working example of your code, so we can check for syntax ... for now it is hard to tell what went wrong.您可以发布代码的最小工作示例,这样我们就可以检查语法......现在很难判断出了什么问题。

Apart from that two small observations: Your input shape in the CNN does not yet show the right shape fitting your data.除了这两个小观察:您在 CNN 中的输入形状尚未显示适合您的数据的正确形状。 And the last dense softmax layer is also not yet what your proposed prediction format looks like.最后一个密集的 softmax 层也不是您提出的预测格式的样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法训练一个 CNN 模型,保存这个 CNN 的权重,然后使用这个权重来为其他训练数据重新训练这个 CNN? - Is there a way to train a CNN model, save the weights of this CNN, and then use this weights to retrain this CNN for other train data? 使用子文件夹中的图像训练 CNN 模型(Train、Val、Test) - Train CNN model with images in sub-folders (Train, Val, Test) 无法在朴素贝叶斯中训练模型 - Unable to train model in Naive Bayes 如何格式化目标以训练Keras CNN模型? - How do I format my targets to train a Keras CNN model? 无法使用CNN训练手势模型(ASL) - Can't train model for hand gesture (ASL) by using CNN TypeError:'NoneType' object 在我训练 CNN model 时不可下标 - TypeError: 'NoneType' object is not subscriptable when I train a CNN model 训练具有多个文件夹和子文件夹的 CNN 模型 - Train CNN model with multiple folders and sub-folders 如何使用 Glove 和 CNN 配置和训练模型进行文本分类? - How to configure and train the model using Glove and CNN for text classification? 我可以同时用两个图像数据集训练一个 CNN model - Can I train a CNN model with Two Image dataset at the same time Python 1D CNN model - train_test_split 中的错误 - Python 1D CNN model - Error in train_test_split
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM