【问题标题】:building a CNN using tflearn使用 tflearn 构建 CNN
【发布时间】:2020-03-05 20:04:14
【问题描述】:

我正在尝试使用以下代码使用tflearn 构建一个 CNN

train_images, train_labels, test_images, test_labels = load_dataset()
convnet = input_data(shape=[None, 28, 28, 1], name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 24, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy')

model = tflearn.DNN(convnet)
model.fit(train_images, train_labels, n_epoch=30,
          validation_set=(test_images,  test_labels),
          snapshot_step=500, show_metric=True, run_id='characterOCR')
model.save('CNN.model')

数据集被重塑为以下形状

def load_mnist_images(filename):
    with gzip.open(filename, 'rb') as f:
        data = np.frombuffer(f.read(), np.uint8, offset=16)
        data = data.reshape(-1, 28, 28, 1)
        return data

这是我的数据集,但我基于 MNIST 结构构建它,现在我收到以下错误:

Traceback (most recent call last):
  File "/home/hassan/JPG-PNG-to-MNIST-NN-Format/CNN_network.py", line 56, in <module>
    snapshot_step=500, show_metric=True, run_id='characterOCR')
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/models/dnn.py", line 216, in fit
    callbacks=callbacks)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/helpers/trainer.py", line 339, in fit
    show_metric)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/helpers/trainer.py", line 818, in _train
    feed_batch)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1149, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(?, 24)'

有人有解决办法吗?

【问题讨论】:

    标签: python conv-neural-network tflearn


    【解决方案1】:

    tflearn 很好,我没有解决问题,我只知道错误来自我向全连接层形状 24 提供形状为 64 的数据我不知道这个 64 来自哪里

    但我从tflearn 切换到Keras API,现在它正在工作

    如果有人想知道源代码,请告诉我

    【讨论】:

    • Tensforflow 2.0 是几周前发布的。目前,Keras 是其官方的高级API。不再开发单独的Keras 包。你必须使用tf.keras
    • 我使用 keras 训练的模型怎么样,我可以在 tf.keras 中使用它们
    猜你喜欢
    • 2018-09-10
    • 2017-12-10
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多