我正在使用Tensorflow进行培训,以开发模型以检测邮件是否为垃圾邮件。我正在使用 Python 。
我的训练数据大小为3000行3列,测试数据大小为2700行和3列。
Batch size: 500
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_classes = 2
batch_size = 32
total_batches = int(3000 / batch_size)
hm_epochs = 10
x = tf.placeholder('float')
y = tf.placeholder('float')
hidden_1_layer = {'f_fum': n_nodes_hl1,
'weight': tf.Variable(tf.random_normal([3000, n_nodes_hl1])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'f_fum': n_nodes_hl2,
'weight': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'f_fum': None,
'weight': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
'bias': tf.Variable(tf.random_normal([n_classes])), }
我在编译期间收到此错误:
WARNING:tensorflow:From saving_and_restoring.py:50: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See @{tf.nn.softmax_cross_entropy_with_logits_v2}.
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1322, in _do_call
return fn(*args)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1409, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [32,12], In[1]: [2794,500]
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "saving_and_restoring.py", line 103, in <module>
train_neural_network(x)
File "saving_and_restoring.py", line 89, in train_neural_network
y: np.array(batch_y)})
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 900, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1135, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1316, in _do_run
run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1335, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [32,12], In[1]: [2794,500]
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
Caused by op 'MatMul', defined at:
File "saving_and_restoring.py", line 103, in <module>
train_neural_network(x)
File "saving_and_restoring.py", line 49, in train_neural_network
prediction = neural_network_model(x)
File "saving_and_restoring.py", line 36, in neural_network_model
l1 = tf.add(tf.matmul(data, hidden_1_layer['weight']), hidden_1_layer['bias'])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py", line 2122, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 4279, in mat_mul
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1718, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Matrix size-incompatible: In[0]: [32,12], In[1]: [2794,500]
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
Python版本 3.6 和 我正在使用 nltk 进行情感分析。
请帮忙。
感谢。
修改
train_set_shuffled.csv shape=(2792,3)
我的代码:
import tensorflow as tf
import pickle
import numpy as np
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_classes = 2
columns=3
batch_size = 32
total_batches = int(3000 / batch_size)
hm_epochs = 10
x = tf.placeholder( tf.float32)
y = tf.placeholder( tf.float32)
hidden_1_layer = {'f_fum': n_nodes_hl1,
'weight': tf.Variable(tf.random_normal([3000, n_nodes_hl1])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'f_fum': n_nodes_hl2,
'weight': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'f_fum': None,
'weight': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
'bias': tf.Variable(tf.random_normal([n_classes])), }
def neural_network_model(data):
l1 = tf.add(tf.matmul(data, hidden_1_layer['weight']), hidden_1_layer['bias'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weight']), hidden_2_layer['bias'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2, output_layer['weight']) + output_layer['bias']
return output
saver = tf.train.Saver()
tf_log = 'tf.log'
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
try:
epoch = int(open(tf_log, 'r').read().split('\n')[-2]) + 1
print('STARTING:', epoch)
except:
epoch = 1
while epoch <= hm_epochs:
if epoch != 1:
saver.restore(sess, "model.ckpt")
epoch_loss = 1
with open('lexicon-2500-2638.pickle', 'rb') as f:
lexicon = pickle.load(f)
with open('train_set_shuffled.csv', buffering=20000, encoding='latin-1') as f:
batch_x = []
batch_y = []
batches_run = 0
for line in f:
label = line.split(':::')[0]
tweet = line.split(':::')[1]
current_words = word_tokenize(tweet.lower())
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())
# OR DO +=1, test both
features[index_value] += 1
line_x = list(features)
line_y = eval(label)
batch_x.append(line_x)
batch_y.append(line_y)
if len(batch_x) >= batch_size:
_, c = sess.run([optimizer, cost], feed_dict={x: np.array(batch_x),
y: np.array(batch_y)})
epoch_loss += c
batch_x = []
batch_y = []
batches_run += 1
print('Batch run:', batches_run, '/', total_batches, '| Epoch:', epoch, '| Batch Loss:', c, )
saver.save(sess, "model.ckpt")
print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)
with open(tf_log, 'a') as f:
f.write(str(epoch) + '\n')
epoch += 1
train_neural_network(x)
def test_neural_network():
prediction = neural_network_model(x)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
try:
saver.restore(sess, "model.ckpt")
except Exception as e:
print(str(e))
epoch_loss = 0
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
feature_sets = []
labels = []
counter = 0
with open('processed-test-set.csv', buffering=20000) as f:
for line in f:
try:
features = list(eval(line.split('::')[0]))
label = list(eval(line.split('::')[1]))
feature_sets.append(features)
labels.append(label)
counter += 1
except:
pass
print('Tested', counter, 'samples.')
test_x = np.array(feature_sets)
test_y = np.array(labels)
print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))
test_neural_network()
编辑2
feature_colum_size=12
x = tf.placeholder(tf.float32,shape=[batch_size,feature_colum_size])
y = tf.placeholder(tf.float32,shape=[batch_size,feature_colum_size])
hidden_1_layer = {'f_fum': n_nodes_hl1,
'weight': tf.Variable(tf.random_normal([feature_colum_size, n_nodes_hl1])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'f_fum': n_nodes_hl2,
'weight': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'bias': tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'f_fum': None,
'weight': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
'bias': tf.Variable(tf.random_normal([n_classes])), }
答案 0 :(得分:0)
我认为问题在于您生成输入数据(batch_x)。您的输入形状似乎是[batch_size,12]
,您将[hidden_layer1["weight"]
]形状的隐藏层(3000,n_nodes_hl1
)相乘(matmul),导致矩阵多重操作失败。
从train_shuffled读取和解析行的方式让我觉得输入要素大小(12)不一致。
我认为你应该做什么。
修复input_feature大小
x = tf.placeholder('float')
至x = tf.placeholder(tf.float32,shape=[batch_size,feature_colum_size])
更改'weight': tf.Variable(tf.random_normal([3000, n_nodes_hl1]))
到
'weight': tf.Variable(tf.random_normal([feature_column_size, n_nodes_hl1])),
feature_column_size应该在输入和第一个隐藏层之间保持一致,以便能够执行matmul乘法。