Tensorflow 中的形状不匹配
Shape Mismatch in Tensorflow
我对 TensorFlow 比较陌生,正在尝试在生产环境中实施我的第一个模型。该模型经过良好的训练和测试,但我发现使用此算法转移到生产中非常具有挑战性。谁能告诉我为什么我的评估行会出现以下错误?
ValueError: Cannot feed value of shape (1, 1095277) for Tensor 'input:0', which has shape '(?, 2912)'
我正在实现的代码是(我尝试了各种不同的方法来让它工作):
哪个张量的长度为 1x1095277?
def use_neural_network(input_data, lexicon,stopWords):
x= tf.placeholder('float', shape=[None, 2912], name='input')
y= tf.placeholder('float', name='output')
#x = tf.Variable('float', [None, 2912]', name='input')
#y = tf.Variable('float', name='output')
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([2912, 1])),'biases':tf.Variable(tf.random_normal([1]))}
output_layer = {'weights':tf.Variable(tf.random_normal([1, 2])),'biases':tf.Variable(tf.random_normal([2])),}
def neural_network_model(data):
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
output = tf.matmul(l1,output_layer['weights']) + output_layer['biases']
return output
prediction = neural_network_model(x)
saver=tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"model.ckpt")
lemmatizer = WordNetLemmatizer()
current_words = word_tokenize(input_data.lower())
current_words = [re.sub("[^a-zA-Z]"," ", i) for i in current_words]
current_words = [re.sub("\s{1,10}"," ", i) for i in current_words]
current_words = [i for i in current_words if i not in stopWords]
current_words = [lemmatizer.lemmatize(i) for i in current_words]
features = np.zeros(len(lexicon))
for word in current_words:
if word.lower() in lexicon:
index_value = lexicon.index(word.lower())
features[index_value] += 1
print(pd.Series(features).sum())
features = np.array(list(features))
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[features]}),1)))
if result[0] == 0:
print('No:',input_data)
elif result[0] == 1:
print('Yes:',input_data)
with open('lexicon_1.pickle','rb') as f:
lexicon = pickle.load(f)
stopWords = set(stopwords.words('english'))
use_neural_network('I do not understand the problem', lexicon, stopWords)
您的网络似乎需要大小为 [2912, 1]
的输入,如 hidden_1_layer
所定义
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([2912, 1])), ...
当您调用预测时,您不会使用大小为 [2912, 1]
的输入来调用它,而是使用等于您词典长度的输入来调用它,该词典(可能)包含 1095277 个数字。
features = np.zeros(len(lexicon))
我还怀疑您将 features
数组包装了两次,首先是 features = np.array(list(features))
,然后是 x:[features]
。对你的数据不是很自信,但感觉不对。
就个人而言,我发现从教程中复制并修改行比尝试从头开始编写更容易学习。
我对 TensorFlow 比较陌生,正在尝试在生产环境中实施我的第一个模型。该模型经过良好的训练和测试,但我发现使用此算法转移到生产中非常具有挑战性。谁能告诉我为什么我的评估行会出现以下错误?
ValueError: Cannot feed value of shape (1, 1095277) for Tensor 'input:0', which has shape '(?, 2912)'
我正在实现的代码是(我尝试了各种不同的方法来让它工作):
哪个张量的长度为 1x1095277?
def use_neural_network(input_data, lexicon,stopWords):
x= tf.placeholder('float', shape=[None, 2912], name='input')
y= tf.placeholder('float', name='output')
#x = tf.Variable('float', [None, 2912]', name='input')
#y = tf.Variable('float', name='output')
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([2912, 1])),'biases':tf.Variable(tf.random_normal([1]))}
output_layer = {'weights':tf.Variable(tf.random_normal([1, 2])),'biases':tf.Variable(tf.random_normal([2])),}
def neural_network_model(data):
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
output = tf.matmul(l1,output_layer['weights']) + output_layer['biases']
return output
prediction = neural_network_model(x)
saver=tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"model.ckpt")
lemmatizer = WordNetLemmatizer()
current_words = word_tokenize(input_data.lower())
current_words = [re.sub("[^a-zA-Z]"," ", i) for i in current_words]
current_words = [re.sub("\s{1,10}"," ", i) for i in current_words]
current_words = [i for i in current_words if i not in stopWords]
current_words = [lemmatizer.lemmatize(i) for i in current_words]
features = np.zeros(len(lexicon))
for word in current_words:
if word.lower() in lexicon:
index_value = lexicon.index(word.lower())
features[index_value] += 1
print(pd.Series(features).sum())
features = np.array(list(features))
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[features]}),1)))
if result[0] == 0:
print('No:',input_data)
elif result[0] == 1:
print('Yes:',input_data)
with open('lexicon_1.pickle','rb') as f:
lexicon = pickle.load(f)
stopWords = set(stopwords.words('english'))
use_neural_network('I do not understand the problem', lexicon, stopWords)
您的网络似乎需要大小为 [2912, 1]
的输入,如 hidden_1_layer
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([2912, 1])), ...
当您调用预测时,您不会使用大小为 [2912, 1]
的输入来调用它,而是使用等于您词典长度的输入来调用它,该词典(可能)包含 1095277 个数字。
features = np.zeros(len(lexicon))
我还怀疑您将 features
数组包装了两次,首先是 features = np.array(list(features))
,然后是 x:[features]
。对你的数据不是很自信,但感觉不对。
就个人而言,我发现从教程中复制并修改行比尝试从头开始编写更容易学习。