【发布时间】:2018-11-11 14:42:42
【问题描述】:
我收到一个错误,ValueError:无法为形状为“(?, 2, 4, 104)”的张量 u'InputData/X:0' 提供形状 (2, 4) 的值。 我写了代码,
# 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)'
我改写成
trainDataSet = np.array([[0.25,0.25,1,1],[0,0,1,1],[0.25,0.25,1,1]])
trainLabel = np.array([[0,1],[0,1],[1,0]])
但同样的错误发生了。我的代码有什么问题?我应该如何解决这个问题?
【问题讨论】:
-
104的形状是什么?
-
@Geeocode 104 是
net = input_data(shape=[2, 4, 104])的这104个@ -
当然,但是你有一个 trainDataSet shape(3,4)
-
您必须提供更多信息,说明您对 trainlabel 的预期输出以及 104 代表什么等。
标签: python tensorflow tflearn