ValueError:无法为形状为'(?,2,4的张量u'InputData / X:0'输入形状(2,4)的值

时间:2018-11-11 14:42:42

标签: python tensorflow tflearn

我收到一个错误,ValueError:无法为张量u'InputData / X:0'输入形状(2,4)的值,其形状为'(?,2,4,104)'。 我写了代码,

sample_matrix_180

追踪说

# coding: utf-8
import tensorflow as tf
import tflearn

from tflearn.layers.core import input_data,dropout,fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

import pandas as pd
import numpy as np
from sklearn import metrics

tf.reset_default_graph()
net = input_data(shape=[2, 4, 104])
net = conv_2d(net, 4, 16, activation='relu')
net = max_pool_2d(net, 1)
net = tflearn.activations.relu(net)
net = dropout(net, 0.5)
net = tflearn.fully_connected(net, 10, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.5, loss='categorical_crossentropy')

model = tflearn.DNN(net)

trainDataSet = [[0.25,0.25,1,1],[0,0,1,1],[0.25,0.25,1,1]]
trainLabel = [[0,1],[0,1],[1,0]]
model.fit(trainDataSet, trainLabel, n_epoch=100, batch_size=32, validation_set=0.1, show_metric=True)

我改写成

Traceback (most recent call last):
  File "cnn.py", line 16, in <module>
    model.fit(trainDataSet, trainLabel, n_epoch=100, batch_size=32, validation_set=0.1, show_metric=True)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tflearn/models/dnn.py", line 216, in fit
    callbacks=callbacks)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 339, in fit
    show_metric)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 818, in _train
    feed_batch)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1128, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2, 4) for Tensor u'InputData/X:0', which has shape '(?, 2, 4, 104)'

但发生相同的错误。我的代码有什么问题?应如何解决?

1 个答案:

答案 0 :(得分:0)

Tensorflow文档中引用:

tflearn.layers.conv.conv_2d 
  

输入:

     

4-D张量 [批次,高度,宽度,in_channels]。

来自其他Tensorflow文档:

tf.nn.conv2d
  

根据给定的 4-D输入和滤波张量,计算二维卷积。

     

给出形状为[batch,in_height,in_width,   in_channels]和形状为[filter_height,   filter_width,in_channels,out_channels],此操作执行   以下:

您的数据集,标签和输入形状不对齐,即彼此不匹配。

当前您的trainDataSet的形状为(3,4):

import numpy as np
trainDataSet = np.array([[0.25,0.25,1,1],[0,0,1,1],[0.25,0.25,1,1]])
print(trainDataSet.shape)

退出:

(3, 4)

但是您将输入形状定义为:

net = input_data(shape=[2, 4, 104])

您真正想要实现的模棱两可,但是如果您想看一个简单的工作示例,则您的代码应该如下所示:

import tensorflow as tf
import tflearn

from tflearn.layers.core import input_data,dropout,fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

import pandas as pd
import numpy as np
from sklearn import metrics

tf.reset_default_graph()
net = input_data(shape=[3, 4, 1])
net = conv_2d(net, 4, 16, activation='relu')
net = max_pool_2d(net, 1)
net = tflearn.activations.relu(net)
net = dropout(net, 0.5)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.5, loss='categorical_crossentropy')

model = tflearn.DNN(net)

trainDataSet = [
    [
        [[0.25], [0.25], [1], [1]],
        [[0], [0], [1], [1]],
        [[0.25], [0.25], [1], [1]]
    ],
    [
        [[0.25], [0.25], [1], [1]],
        [[0], [0], [1], [1]],
        [[0.25], [0.25], [1], [1]]
    ],
    [
        [[0.25], [0.25], [1], [1]],
        [[0], [0], [1], [1]],
        [[0.25], [0.25], [1], [1]]
    ]
]

trainLabel = [[0,1],[0,1],[1,0]]
model.fit(trainDataSet, trainLabel, n_epoch=100, batch_size=32, validation_set=0.1, show_metric=True)

退出:

---------------------------------
Run id: NHHJV7
Log directory: /tmp/tflearn_logs/
INFO:tensorflow:Summary name Accuracy/ (raw) is illegal; using Accuracy/__raw_ instead.
---------------------------------
Training samples: 2
Validation samples: 1
--
Training Step: 1  | time: 1.160s
| Adam | epoch: 001 | loss: 0.00000 - acc: 0.0000 | val_loss: 23.02585 - val_acc: 0.0000 -- iter: 2/2
--
Training Step: 2  | total loss: 0.62966 | time: 1.008s
| Adam | epoch: 002 | loss: 0.62966 - acc: 0.0000 | val_loss: 10.76885 - val_acc: 0.0000 -- iter: 2/2
.
.
.
Training Step: 99  | total loss: 0.00000 | time: 1.013s
| Adam | epoch: 099 | loss: 0.00000 - acc: 1.0000 | val_loss: 23.02585 - val_acc: 0.0000 -- iter: 2/2
--
Training Step: 100  | total loss: 0.00000 | time: 1.011s
| Adam | epoch: 100 | loss: 0.00000 - acc: 1.0000 | val_loss: 23.02585 - val_acc: 0.0000 -- iter: 2/2
--