我正在做一个基本的由Tensorflow驱动的CNN。我无法找到某种尺寸。预先感谢
我正在使用系统中的 jupyter 。我在 miniconda 环境中运行。
pred = conv_net(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)**
在这里检查预测图像的最大值的索引是否等于实际标记的图像。和 两者都是列向量。
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
计算所有给定图像的准确度并将其平均。
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
the refernce link from where i am learning
错误是
InvalidArgumentError Traceback (most recent call last)
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1627 try:
-> 1628 c_op = c_api.TF_FinishOperation(op_desc)
1629 except errors.InvalidArgumentError as e:
InvalidArgumentError: Dimensions must be equal, but are 11 and 10 for 'Add_1' (op: 'Add') with input shapes: [?,11], [10].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-9-b95a9f9c020a> in <module>()
----> 1 pred = conv_net(x, weights, biases)
2
3 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
4
5 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
<ipython-input-8-a23543908ef7> in conv_net(x, weights, biases)
24 # Output, class prediction
25 # finally we multiply the fully connected layer with the weights and add a bias term.
---> 26 out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
27 return out
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in add(x, y, name)
308 if _ctx is None or not _ctx._eager_context.is_eager:
309 _, _, _op = _op_def_lib._apply_op_helper(
--> 310 "Add", x=x, y=y, name=name)
311 _result = _op.outputs[:]
312 _inputs_flat = _op.inputs
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
785 op = g.create_op(op_type_name, inputs, output_types, name=scope,
786 input_types=input_types, attrs=attr_protos,
--> 787 op_def=op_def)
788 return output_structure, op_def.is_stateful, op
789
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
486 'in a future version' if date is None else ('after %s' % date),
487 instructions)
--> 488 return func(*args, **kwargs)
489 return tf_decorator.make_decorator(func, new_func, 'deprecated',
490 _add_deprecated_arg_notice_to_docstring(
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\framework\ops.py in create_op(***failed resolving arguments***)
3272 input_types=input_types,
3273 original_op=self._default_original_op,
-> 3274 op_def=op_def)
3275 self._create_op_helper(ret, compute_device=compute_device)
3276 return ret
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1790 op_def, inputs, node_def.attr)
1791 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1792 control_input_ops)
1793
1794 # Initialize self._outputs.
~\Miniconda3\envs\idp\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1629 except errors.InvalidArgumentError as e:
1630 # Convert to ValueError for backwards compatibility.
-> 1631 raise ValueError(str(e))
1632
1633 return c_op
ValueError: Dimensions must be equal, but are 11 and 10 for 'Add_1' (op: 'Add') with input shapes: [?,11], [10].
以下是完整代码:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
df = pd.read_excel(r"C:/Users/ggmah/Desktop/HMM Data updated.xlsx")
tf.logging.set_verbosity(tf.logging.INFO)
dff = OneHotEncoder(df)
dfg = pd.get_dummies(df)
o =list(df.columns.values)
label_dict = dict()
for i,value in enumerate(o):
label_dict[i] = value
training_iters = 220
learning_rate = 0.002
batch_size = 16
n_input = 59
n_classes = 11
x = tf.placeholder("float", [None, 60,11,1])
y = tf.placeholder("float", [None, n_classes])
def conv2d(x, W, b, strides=1):
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],padding='SAME')
weights = {
'wc1': tf.get_variable('W0', shape=(3,3,1,32), initializer=tf.contrib.layers.xavier_initializer()),
'wc2': tf.get_variable('W1', shape=(3,3,32,64), initializer=tf.contrib.layers.xavier_initializer()),
'wc3': tf.get_variable('W2', shape=(3,3,64,128), initializer=tf.contrib.layers.xavier_initializer()),
'wd1': tf.get_variable('W3', shape=(4*4*128,128), initializer=tf.contrib.layers.xavier_initializer()),
'out': tf.get_variable('W6', shape=(128,n_classes), initializer=tf.contrib.layers.xavier_initializer()),
}
biases = {
'bc1': tf.get_variable('B0', shape=(32), initializer=tf.contrib.layers.xavier_initializer()),
'bc2': tf.get_variable('B1', shape=(64), initializer=tf.contrib.layers.xavier_initializer()),
'bc3': tf.get_variable('B2', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'bd1': tf.get_variable('B3', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'out': tf.get_variable('B4', shape=(10), initializer=tf.contrib.layers.xavier_initializer()),
}
X = df[['Att1','Att2','Att3','Att4','Att5','Att6','Att7','Att8','Att9','Att10']]
Y = df[['Att11']]
train_X, test_X,train_y,test_y = train_test_split(X,Y,train_size=0.88,random_state=5)
def conv_net(x, weights, biases):
#here we call the conv2d function we had defined above and pass the input image x, weights wc1 and bias bc1.
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
#Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 14*14 matrix.
conv1 = maxpool2d(conv1, k=2)
#Convolution Layer
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
conv2 = maxpool2d(conv2, k=2)
conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
conv3 = maxpool2d(conv3, k=2)
fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
#Output, class prediction
# finally we multiply the fully connected layer with the weights and add a bias term.
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
pred = conv_net(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#Here you check whether the index of the maximum value of the predicted image is equal to the actual labelled image. and
#both will be a column vector.
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
#calculate accuracy across all the given images and average them out.
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
答案 0 :(得分:0)
这只是尺寸上的错误。最后一个out
层的偏差形状应为[11]
,因为您有11个类的输出连接到11个softmax:
biases = {
'bc1': tf.get_variable('B0', shape=(32), initializer=tf.contrib.layers.xavier_initializer()),
'bc2': tf.get_variable('B1', shape=(64), initializer=tf.contrib.layers.xavier_initializer()),
'bc3': tf.get_variable('B2', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'bd1': tf.get_variable('B3', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'out': tf.get_variable('B4', shape=(11), initializer=tf.contrib.layers.xavier_initializer()),
}