Python 3.x Tensorflow中的登录和标签不匹配
在一次热编码后,Tensorflow中的logits和标签之间存在不匹配。 我的批量是256。如何在标签Tensor中获得批量大小?我猜这个问题与LabelEncoder和一个热编码器有关。任何帮助都是值得的 请在下面查找代码Python 3.x Tensorflow中的登录和标签不匹配,python-3.x,tensorflow,scikit-learn,Python 3.x,Tensorflow,Scikit Learn,在一次热编码后,Tensorflow中的logits和标签之间存在不匹配。 我的批量是256。如何在标签Tensor中获得批量大小?我猜这个问题与LabelEncoder和一个热编码器有关。任何帮助都是值得的 请在下面查找代码 from sklearn import preprocessing le = preprocessing.LabelEncoder() cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logit
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = tf.one_hot(le.fit_transform(labels), n_classes)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learn_rate).minimize(cost)
correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(tf.one_hot(le.fit_transform(labels), n_classes),1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
batchSize = 256
epochs = 20 # 200epoch+.5lr = 99.6
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
total_batches = batches(batchSize, train_features, train_labels)
for epoch in range(epochs):
for batch_features, batch_labels in total_batches:
train_data = {features: batch_features, labels : batch_labels, keep_prob : 0.5}
sess.run(optimizer, feed_dict = train_data)
# Print status for every 100 epochs
if epoch % 10 == 0:
valid_accuracy = sess.run(
accuracy,
feed_dict={
features: val_features,
labels: val_labels,
keep_prob : 0.5})
print('Epoch {:<3} - Validation Accuracy: {}'.format(
epoch,
valid_accuracy))
Accuracy = sess.run(accuracy, feed_dict={features : test_features, labels :test_labels, keep_prob : 1.0})
# Save the model
saver.save(sess, save_file)
print('Trained Model Saved.')
prediction=tf.argmax(logits,1)
output_array = le.inverse_transform(prediction.eval(feed_dict={features : test_features, keep_prob: 1.0}))
prediction = np.reshape(prediction, (test_features.shape[0],1))
np.savetxt("prediction.csv", prediction, delimiter=",")
从sklearn导入预处理
le=预处理。LabelEncoder()
cost=tf.reduce\u mean(tf.nn.softmax\u cross\u entropy\u with\u logits(logits=logits,labels=tf.one\u hot(le.fit\u transform(labels),n类)))
优化器=tf.train.GradientDescentOptimizer(学习率=学习率)。最小化(成本)
正确的预测=tf.equal(tf.argmax(logits,1),tf.argmax(tf.one_hot(le.fit_变换(标签),n_类),1))
准确度=tf.reduce_平均值(tf.cast(正确的预测,tf.float32))
batchSize=256
纪元=20#200纪元+0.5lr=99.6
init=tf.global_variables_initializer()
使用tf.Session()作为sess:
sess.run(初始化)
总批次=批次(批次大小、系列特征、系列标签)
对于范围内的历元(历元):
对于批次特征,批次标签在总批次中:
train_data={features:batch_features,labels:batch_labels,keep_prob:0.5}
sess.run(优化器,feed\u dict=train\u数据)
#每100个纪元打印一次状态
如果历元%10==0:
有效精度=sess.run(
精确
饮食={
特征:val_特征,
标签:val_标签,
保持_prob:0.5})
print('Epoch{:的问题是tf.one_hot(le.fit_transform(标签),n_类)
这将传递一个需要numpy数组的张量。为此张量调用eval()后,问题得到解决。您是否检查了批处理标签的形状,是256?是的,Vijay。批处理标签是256。登录应该是tf.one_hot(标签,n类)以获取(256,n类)成本=tf。使用登录减少平均值(tf.nn.softmax)交叉熵(logits=tf.one_-hot(labels,n_-classes)),labels=tf.one_-hot(labels,n_-classes)))正如您所说,但是对于错误计算,我们需要使用labels和logits。我真的不知道如何使用这样的方法。
InvalidArgumentError: logits and labels must be same size: logits_size=[256,1161] labels_size=[1,1161]
[[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape, Reshape_1)]]
Caused by op 'SoftmaxCrossEntropyWithLogits', defined at:
File "C:\Anaconda\envs\gpu\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Anaconda\envs\gpu\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
app.launch_new_instance()
File "C:\Anaconda\envs\gpu\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "C:\Anaconda\envs\gpu\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\Anaconda\envs\gpu\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "C:\Anaconda\envs\gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\Anaconda\envs\gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\Anaconda\envs\gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\Anaconda\envs\gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\Anaconda\envs\gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Anaconda\envs\gpu\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Anaconda\envs\gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2698, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Anaconda\envs\gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2802, in run_ast_nodes
if self.run_code(code, result):
File "C:\Anaconda\envs\gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-9a6fe2134e3e>", line 52, in <module>
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = tf.one_hot(le.fit_transform(labels), n_classes)))
File "C:\Anaconda\envs\gpu\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1594, in softmax_cross_entropy_with_logits
precise_logits, labels, name=name)
File "C:\Anaconda\envs\gpu\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 2380, in _softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "C:\Anaconda\envs\gpu\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
File "C:\Anaconda\envs\gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Anaconda\envs\gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[256,1161] labels_size=[1,1161]
[[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape, Reshape_1)]]