## 你好，我想用CNN训练一个输入为[345,9] 输出为[4] 的模型，因为输入矩阵的行和列不相等，这样行吗？

wcsmomo 发布于 2016/11/18 09:49

@zhuyuping 你好，想跟你请教个问题：我看了你写的关于tensorflow的十几篇博文，受益匪浅。 我想用CNN训练一个输入为[345,9] 输出为[4] 的模型,但训练的时候报错了。我是个初学者，不太清楚输入矩阵的行和列不相等能不能用CNN训练？如果能的话，我应该修改那些参数呢？

#import input_data
import MySQLdb

import tensorflow as tf

# Parameters
learning_rate = 0.001
training_iters = 200000
batch_size = 256
display_step = 10

# Network Parameters
n_input = 3105 # MNIST data input (img shape: 28*28)
n_classes = 4 # MNIST total classes (0-9 digits)
dropout = 0.9 # Dropout, probability to keep units

# tf Graph input
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)

# Create model
def conv2d(img, w, b):

def max_pool(img, k):
return tf.nn.max_pool(img, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')

def conv_net(_X, _weights, _biases, _dropout):
# Reshape input picture
print _X
_X = tf.reshape(_X, shape=[ 345, 9])

# Convolution Layer
print _X
print _weights
print _biases
print _dropout
conv1 = conv2d(_X, _weights['wc1'], _biases['bc1'])
# Max Pooling (down-sampling)
conv1 = max_pool(conv1, k=2)
# Apply Dropout
conv1 = tf.nn.dropout(conv1, _dropout)

# Convolution Layer
conv2 = conv2d(conv1, _weights['wc2'], _biases['bc2'])
# Max Pooling (down-sampling)
conv2 = max_pool(conv2, k=2)
# Apply Dropout
conv2 = tf.nn.dropout(conv2, _dropout)

# Fully connected layer
dense1 = tf.reshape(conv2, [-1, _weights['wd1'].get_shape().as_list()[0]]) # Reshape conv2 output to fit dense layer input
dense1 = tf.nn.relu(tf.add(tf.matmul(dense1, _weights['wd1']), _biases['bd1'])) # Relu activation
dense1 = tf.nn.dropout(dense1, _dropout) # Apply Dropout

# Output, class prediction
return out

# Store layers weight & bias
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), # 5x5 conv, 1 input, 32 outputs
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # 5x5 conv, 32 inputs, 64 outputs
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])), # fully connected, 7*7*64 inputs, 1024 outputs
'out': tf.Variable(tf.random_normal([1024, n_classes])) # 1024 inputs, 10 outputs (class prediction)
}

biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
pred = conv_net(x, weights, biases, keep_prob)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()

tf.scalar_summary("loss", cost)
tf.scalar_summary("accuracy", accuracy)
# Merge all summaries to a single operator
merged_summary_op = tf.merge_all_summaries()

# Launch the graph
with tf.Session() as sess:
sess.run(init)
summary_writer = tf.train.SummaryWriter('/tmp/logs', graph_def=sess.graph_def)
step = 1
conn= MySQLdb.connect(
host='172.16.1.27',
port = 3306,
user='root',
passwd='520a520a',
db ='infanglogic',)
cur = conn.cursor()
cur.execute("select * from rb1701_k_1")
results = cur.fetchall()
count = 0
isFirst = 1
date = 0
batch_xs = []
batch_ys = []
# Keep training until reach max iterations

for k in results:
xs = [k[1],k[2],k[3],k[4],k[5],k[6],k[7],k[8],k[9]]
batch_xs.append(xs)
count = count + 1
if count == 345:
break
i = 345
while i < len(results) - 1:
print " i = "+str(i)
if i == 345:
kline = results[i+1]
batch_ys = [kline[10],kline[11],kline[12],kline[13]]
i = i + 1
else:
print " i__ = "+str(i)
kline = results[i]
del batch_xs[0]
xs = [kline[1],kline[2],kline[3],kline[4],kline[5],kline[6],kline[7],kline[8],kline[9]]
batch_xs.append(xs)
kline1 = results[i+1]
batch_ys = [kline1[10],kline1[11],kline1[12],kline1[13]]
i = i + 1
print "len(batch_xs)" + str(len(batch_xs))
print batch_xs
print "batch_ys" + str(len(batch_ys))
print batch_ys

sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
if step % display_step == 0:
# Calculate batch accuracy
acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
# Calculate batch loss
loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)
summary_str = sess.run(merged_summary_op, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})

print "Optimization Finished!"
# Calculate accuracy for 256 mnist test images
#print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})

Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/cProfile.py", line 199, in <module>
main()
File "/usr/lib/python2.7/cProfile.py", line 192, in main
runctx(code, globs, None, options.outfile, options.sort)
File "/usr/lib/python2.7/cProfile.py", line 49, in runctx
prof = prof.runctx(statement, globals, locals)
File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
exec cmd in globals, locals
File "/home/wcsmomo/cnnTest/cnn.py", line 81, in <module>
pred = conv_net(x, weights, biases, keep_prob)
File "/home/wcsmomo/cnnTest/cnn.py", line 43, in conv_net
conv1 = conv2d(_X, _weights['wc1'], _biases['bc1'])
File "/home/wcsmomo/cnnTest/cnn.py", line 28, in conv2d
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 295, in conv2d
data_format=data_format, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 202, in conv2d_shape
input_shape = op.inputs[0].get_shape().with_rank(4)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 625, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (345, 9) must have rank 4