TensorFlow: dimension error. how to debug? -
i'm beginner tf
i've tried adapt code working other data (nomnist) new data, , have dimensionality error, , don't know how deal it.
to debug, i'm trying use tf.shape
method doesn't give me info need...
def reformat(dataset, labels): #dataset = dataset.reshape((-1, num_var)).astype(np.float32) # map 2 [0.0, 1.0, 0.0 ...], 3 [0.0, 0.0, 1.0 ...] labels = (np.arange(num_labels) == labels[:,none]).astype(np.float32) return dataset, labels train_dataset, train_labels = reformat(train_dataset, train_labels) valid_dataset, valid_labels = reformat(valid_dataset, valid_labels) test_dataset, test_labels = reformat(test_dataset, test_labels) print('training set', train_dataset.shape, train_labels.shape) print('validation set', valid_dataset.shape, valid_labels.shape) print('test set', test_dataset.shape, test_labels.shape) type(train_dataset)
training set (790184, 29) (790184, 39) validation set (43899, 29) (43899, 39) test set (43899, 29) (43899, 39)
# adding regularization 1 hidden layer network graph1 = tf.graph() batch_size = 128 num_steps=3001 import datetime starttime = datetime.datetime.now() def define_and_run_batch(beta): num_relu =1024 graph1.as_default(): # input data. training data, use placeholder fed # @ run time training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_var)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # variables. weights_relu = tf.variable( tf.truncated_normal([num_var, num_relu])) print(tf.shape(weights_relu) ) biases_relu = tf.variable(tf.zeros([num_relu])) weights_layer1 = tf.variable( tf.truncated_normal([num_relu, num_labels])) biases_layer1 = tf.variable(tf.zeros([num_labels])) # training computation. logits_relu = tf.matmul(tf_train_dataset, weights_relu) + biases_relu relu_vec = tf.nn.relu(logits_relu) logits_layer = tf.matmul(relu_vec, weights_layer1) + biases_layer1 # loss = tf.reduce_mean( # tf.nn.softmax_cross_entropy_with_logits(logits_layer, tf_train_labels)) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits_layer, tf_train_labels,name="cross_entropy") l2reg = tf.reduce_sum(tf.square(weights_relu))+tf.reduce_sum(tf.square(weights_layer1)) # beta = 0.005 loss = tf.reduce_mean(cross_entropy+beta*l2reg) # optimizer. optimizer = tf.train.gradientdescentoptimizer(0.3).minimize(loss) # predictions training, validation, , test data. train_prediction = tf.nn.softmax(logits_layer) print("ok") print(tf.shape(weights_relu) ) valid_prediction = tf.nn.softmax( tf.matmul(tf.nn.relu((tf.matmul(tf_valid_dataset, weights_relu) + biases_relu)),weights_layer1)+biases_layer1) test_prediction =tf.nn.softmax( tf.matmul(tf.nn.relu((tf.matmul(tf_test_dataset, weights_relu) + biases_relu)),weights_layer1)+biases_layer1) tf.session(graph=graph1) session: tf.initialize_all_variables().run() print("initialized") step in range(num_steps): # pick offset within training data, has been randomized. # note: use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # generate minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # prepare dictionary telling session feed minibatch. # key of dictionary placeholder node of graph fed, # , value numpy array feed it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} # _, l, predictions, logits = session.run( [optimizer, loss,train_prediction,logits_relu], feed_dict=feed_dict) if (step % 500 == 0): print("minibatch loss @ step %d: %f" % (step, l)) print("minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) test_acc = accuracy(test_prediction.eval(), test_labels) print("test accuracy: %.1f%%" % test_acc) print('loss=%s' % l) x = datetime.datetime.now() - starttime print(x) return(test_acc,round(l,5)) define_and_run_batch(0.005)
tensor("shape:0", shape=(2,), dtype=int32) ok tensor("shape_1:0", shape=(2,), dtype=int32) --------------------------------------------------------------------------- valueerror traceback (most recent call last) in () 94 return(test_acc,round(l,5)) 95 ---> 96 define_and_run_batch(0.005)
in define_and_run_batch(beta) 54 print(tf.shape(weights_relu) ) 55 valid_prediction = tf.nn.softmax( ---> 56 tf.matmul(tf.nn.relu((tf.matmul(tf_valid_dataset, weights_relu) + biases_relu)),weights_layer1)+biases_layer1) 57 58
/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.pyc in matmul(a, b, transpose_a, transpose_b, a_is_sparse, b_is_sparse, name) 949 transpose_a=transpose_a, 950 transpose_b=transpose_b, --> 951 name=name) 952 953 sparse_matmul = gen_math_ops._sparse_mat_mul
/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.pyc in _mat_mul(a, b, transpose_a, transpose_b, name) 684 """ 685 return _op_def_lib.apply_op("matmul", a=a, b=b, transpose_a=transpose_a, --> 686 transpose_b=transpose_b, name=name) 687 688
/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords) 653 op = g.create_op(op_type_name, inputs, output_types, name=scope, 654 input_types=input_types, attrs=attr_protos, --> 655 op_def=op_def) 656 outputs = op.outputs 657 return _restructure(ops.convert_n_to_tensor(outputs), output_structure)
/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device) 2040
original_op=self._default_original_op, op_def=op_def) 2041 if compute_shapes: -> 2042 set_shapes_for_outputs(ret) 2043 self._add_op(ret) 2044
self._record_op_seen_by_control_dependencies(ret)/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op) 1526 raise runtimeerror("no shape function registered standard op: %s" 1527
% op.type) -> 1528 shapes = shape_func(op) 1529 if len(op.outputs) != len(shapes): 1530 raise runtimeerror(/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/ops/common_shapes.pyc in matmul_shape(op) 87 inner_a = a_shape[0] if transpose_a else a_shape[1] 88 inner_b = b_shape[1] if transpose_b else b_shape[0] ---> 89 inner_a.assert_is_compatible_with(inner_b) 90 return [tensor_shape.tensorshape([output_rows, output_cols])] 91
/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.pyc in assert_is_compatible_with(self, other) 92 if not self.is_compatible_with(other): 93 raise valueerror("dimensions %s , %s not compatible" ---> 94 % (self, other)) 95 96 def merge_with(self, other):
valueerror: dimensions dimension(29) , dimension(30) not compatible
the whole code on github https://github.com/faguicurtain/kaggle-sf udacity assignment 3 file working
the original data here https://www.kaggle.com/c/sf-crime/data
in udacity, data images , each image 28x28 matrix reformatted flattened vectors of size 784
in kaggle-sf file, feeding vectors of size 29, , labels can take 39 different values.
thanks help
in debug mode can check shapes of tensors. way error valid_prediction assignment. make better debugging , reading it's better define each step in separate line. using 4 operation in 1 line. btw in debug mode (for example in pycharm) can inspect element , check causing problem
Comments
Post a Comment