Hi. I'm new to Tensorflow and trying to run cifar10 dataset with CNN.
My Network is constructed with three layers such as
Convolution + Max Pooling
Fully Connected Layer
Softmax Layer
Below is my tensorflow code of the model.
15 def model(X, w, w2, w_o, p_keep_conv, p_keep_hidden):
16
17 layer1 = tf.nn.relu(tf.nn.conv2d(X, w,strides=[1, 1, 1, 1], padding='SAME'))
18 layer1 = tf.nn.max_pool(l1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
19
20 layer1 = tf.reshape(l1,[-1,w2.get_shape().as_list()[0]])
21 layer1 = tf.nn.dropout(l1, p_keep_conv)
22
23 layer2 = tf.nn.relu(tf.matmul(layer1, w2))
24 layer2 = tf.nn.dropout(l4, p_keep_hidden)
25
26 pyx = tf.matmul(layer2, w_o)
27 return pyx
28
The input image has [-1, 32, 32, 3] shape.(32*32 pixel, RGB)
Since the filter of max pooling is [1,2,2,1], the stride is [1,2,2,1] and the output channel is 5,
I think the form of weight (w2 in below code) between max pooling layer and fully connected layer need to be [5*16*16*3, 125].
(5: channels, 16: 32/2 pixel, 3: rgb, 125: # of output neuron)
Below is my tensorflow code of the parameters.
60 trX = trX.reshape(-1, 32, 32, 3) # 32x32x3 input img
61 teX = teX.reshape(-1, 32, 32, 3) # 32x32x3 input img
62
63 X = tf.placeholder("float", [None, 32, 32, 3])
64 Y = tf.placeholder("float", [None, 10])
65
66 w = init_weights([5, 5, 3, 5])
67 w2 = init_weights([5*16*16*3, 125])
68 w_o = init_weights([125, 10])
69
70 p_keep_conv = tf.placeholder("float")
71 p_keep_hidden = tf.placeholder("float")
72
73 py_x = model(X, w, w2, w_o, p_keep_conv, p_keep_hidden)
74
75 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
76 #train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
77 train_op = tf.train.AdamOptimizer(1e-4).minimize(cost)
78 predict_op = tf.argmax(py_x, 1)
79
However it show me an error like below.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 715, in _do_call
return fn(*args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 697, in _run_fn
status, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py", line 66, in __exit__
next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/errors.py", line 450, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "convCifar.py", line 99, in <module>
p_keep_conv: 0.8, p_keep_hidden: 0.5})
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 636, in _run
feed_dict_string, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 708, in _do_run
target_list, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 728, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
Caused by op 'Reshape', defined at:
File "convCifar.py", line 82, in <module>
py_x = model(X, w, w4, w_o, p_keep_conv, p_keep_hidden)
File "convCifar.py", line 27, in model
l1 = tf.reshape(l1,[-1,w4.get_shape().as_list()[0]])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1383, in reshape
name=name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__
self._traceback = _extract_stack()
I think the problem is about the dimension of "w2"(weight between max pooling layer and fully connected layer). Also, I can not understand How the 6400 can be occurred.
How can fix the error?
Please let me know if the information is small.
Thank you!
The error tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840 suggests that the input tensor of tf.reshape() in line 20 has a number of values that is not a multiple of 3840.
That's because tensor l1 isn't defined within function model (you might have used it earlier and it might have 6400 values). You probably want to set l1=layer1. Note that tensor l4 isn't defined in function model either.
Please, let me know if my answer doesn't solve your error.
Related
I don't know why this error keep coming when I run the code below
CNN.fit(X_train_vector, y_train, epochs=10)
My CNN code is this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (21367, 9000)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
My "X_train_vector" has a shape:
(21367, 9000)
My "y_train" has a shape:
(21367, 1)
The Error I am getting:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-895976bf38cd> in <module>()
----> 1 CNN.fit(X_train_vector, y_train, epochs=10)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shape=(None, 21367, 9000), found shape=(None, 9000)
I have tried several solutions, including changing my first line of CNN to this:
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
But running it says:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-dd8d734d0a9f> in <module>()
1 CNN = tf.keras.models.Sequential()
----> 2 CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
3 CNN.add(tf.keras.layers.MaxPooling1D(2))
4 CNN.add(tf.keras.layers.Dropout(0.2))
5 CNN.add(tf.keras.layers.Flatten())
3 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
441 else:
442 batch_size = None
--> 443 batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
444 self._batch_input_shape = batch_input_shape
445
TypeError: 'int' object is not iterable
Can anyone help me. I have been looking for the solution for two days, it should work the way I am trying. Is there a mistake I am making? Please let me know.
Thanks In Advance.
The input array should have the shape (None, shape_0, shape_1), where None represent the batch size, and (shape_0, shape_1) represents the shape of the feature. So, you should reshape your input array:
X_train_vector = X_train_vector.reshape(-1, 9000, 1)
And you don't really need to specify the batch size when building the model, so remove that and just use (9000, 1) as the input_shape. Try this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000, 1)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
And this should solve the problem the same error would not appear again.
There are several questions on SO with this error:
ValueError: as_list() is not defined on an unknown TensorShape.
and a few relevant issues on git as well: 1, 2.
However, I haven't found a consistent answer to why this message appears, or a solution for my specific issue. This entire pipeline used to work with tf2.0.0-alpha and now, after installing with Conda conda install tensorflow=2.0 python=3.6 the pipeline is broken.
In short terms, I use a generator to return image data to the tf.data.Dataset.from_generator() method. This works fine, until I try to call the model.fit() method, which results in the following error.
Train for 750 steps, validate for 100 steps
Epoch 1/5
1/750 [..............................] - ETA: 10sTraceback (most recent call last):
File "/usr/local/anaconda3/envs/tf/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/tmo/Projects/casa/image/src/train.py", line 148, in <module>
Trainer().train_vgg16()
File "/Users/tmo/Projects/casa/image/src/train.py", line 142, in train_vgg16
validation_steps=100)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 324, in fit
total_epochs=epochs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 123, in run_one_epoch
batch_outs = execution_function(iterator)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 86, in execution_function
distributed_function(input_fn))
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in __call__
result = self._call(*args, **kwds)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 503, in _call
self._initialize(args, kwds, add_initializers_to=initializer_map)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 408, in _initialize
*args, **kwds))
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1848, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 66, in distributed_function
model, input_iterator, mode)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 112, in _prepare_feed_values
inputs, targets, sample_weights = _get_input_from_iterator(inputs)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py", line 149, in _get_input_from_iterator
distribution_strategy_context.get_strategy(), x, y, sample_weights)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 308, in validate_distributed_dataset_inputs
x_values_list = validate_per_replica_inputs(distribution_strategy, x)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 356, in validate_per_replica_inputs
validate_all_tensor_shapes(x, x_values)
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/keras/distribute/distributed_training_utils.py", line 373, in validate_all_tensor_shapes
x_shape = x_values[0].shape.as_list()
File "/usr/local/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 1171, in as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
This is the code that loads and reshapes each image:
def preprocess_image(self, image):
"""
"""
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, self.hw)
image /= 255.0 # normalize to [0,1] range
image.set_shape([224, 224, 3])
return image
which is applied to a generator (e.g. training_generator) that runs through a list of images and yields the preprocessed result:
def make_ts_dataset(self):
AUTOTUNE = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
image_count_training = len(self.X_train)
image_count_validation = len(self.X_test)
training_generator = GetTensor(hw=self.hw, train=True).make_tensor
training_image_ds = tf.data.Dataset.from_generator(training_generator, tf.float32, [224, 224, 3])
training_price_ds = tf.data.Dataset.from_tensor_slices(tf.cast(self.y_train, tf.float32))
validation_generator = GetTensor(hw=self.hw, test=True).make_tensor
validation_image_ds = tf.data.Dataset.from_generator(validation_generator, tf.float32, [224, 224, 3])
validation_price_ds = tf.data.Dataset.from_tensor_slices(tf.cast(self.y_test, tf.float32))
training_ds = tf.data.Dataset.zip((training_image_ds, training_price_ds))
validation_ds = tf.data.Dataset.zip((validation_image_ds, validation_price_ds))
training_ds = training_ds.shuffle(buffer_size=int(round(image_count_training)))
training_ds = training_ds.repeat()
training_ds = training_ds.batch(BATCH_SIZE)
training_ds = training_ds.prefetch(buffer_size=AUTOTUNE)
validation_ds = validation_ds.shuffle(buffer_size=int(round(image_count_validation)))
validation_ds = validation_ds.repeat()
validation_ds = validation_ds.batch(BATCH_SIZE)
validation_ds = validation_ds.prefetch(buffer_size=AUTOTUNE)
for image_batch, label_batch in training_ds.take(1):
print(label_batch.shape, image_batch.shape)
pass
return training_ds, validation_ds
At all points, the shapes look correct, i.e. (32,) (32, 224, 224, 3)
I am initialising the weights with trained weights from VGG16
def train_vgg16(self):
training_ds, validation_ds = Trainer.make_ts_dataset(self)
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_shape=(224, 224, 3))
base_vgg.trainable = False
print(base_vgg.summary())
vgg_with_base = keras.Sequential([
base_vgg,
tf.keras.layers.GlobalMaxPooling2D(),
tf.keras.layers.Dense(1024, activation=tf.nn.relu),
tf.keras.layers.Dense(1024, activation=tf.nn.relu),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dense(1)])
print(base_vgg.summary())
vgg_with_base.compile(optimizer='adam',
loss='mse',
metrics=['mape'])
vgg_with_base.fit(training_ds,
epochs=5,
validation_data=validation_ds,
steps_per_epoch=750,
validation_steps=100)
However, training never initiates because x_shape = x_values[0].shape.as_list() fails.
Edit (11/12/19):
After some troubleshooting, I found that the error is initiated in the keras.applications layer.
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_shape=(224, 224, 3))
Removing base_vgg from the model and initialising training works fine.
Explicitly defining the shape with Input from tensorflow.keras.layers in the keras.applications.vgg16.VGG16 solved the problem for me.
from tensorflow.keras.layers import Input
base_vgg = keras.applications.vgg16.VGG16(include_top=False,
weights='imagenet',
input_tensor=Input(shape=(224,224,3)),
input_shape=(224, 224, 3))
I still think this is closer to a bug than a feature though.
I fixed this error by tf.cast(label, tf.float32) after importing the masks.
I can't figure out what is causing this error for multi-output Keras Model when using callbacks.TensorBoard.
tbCallBack = keras.callbacks.TensorBoard(log_dir = logdir, histogram_freq = 1, write_graph = 1, write_images = 0, write_grads = 1)
###No errror when not using callbacks
regr.fit( Ax_train, [Ay_train_p, Ay_train_s], validation_data= (Ax_test, [Ay_test_p, Ay_test_s]), epochs = 500, batch_size = 10, verbose = 1)
###No errror when not using validation_data
regr.fit( Ax_train, [Ay_train_p, Ay_train_s], epochs = 500, batch_size = 10, verbose = 1, callbacks=[tbCallBack])
###Error Occurred
regr.fit( Ax_train, [Ay_train_p, Ay_train_s], validation_data= (Ax_test, [Ay_test_p, Ay_test_s]), epochs = 500, batch_size = 10, verbose = 1, callbacks=[tbCallBack])
Error
Epoch 1/500
1280/1663 [======================>.......] - ETA: 0s - loss: 1.6230 - output_power_loss: 0.9627 - output_slack_loss: 0.66032017-07-11 03:17:27.964542: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1] has negative dimensions
2017-07-11 03:17:27.964589: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
[[Node: output_slack_sample_weights = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
2017-07-11 03:17:27.970690: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1] has negative dimensions
2017-07-11 03:17:27.970735: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
[[Node: output_power_sample_weights = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
2017-07-11 03:17:27.972004: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1] has negative dimensions
2017-07-11 03:17:27.972026: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1] has negative dimensions
[[Node: output_power_sample_weights = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):
File "tf_keras.py", line 183, in <module>
regr.fit( Ax_train, [Ay_train_p, Ay_train_s], validation_data= (Ax_test, [Ay_test_p, Ay_test_s]), epochs = 500, batch_size = 10, verbose = 1, callbacks=[tbCallBack])
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/engine/training.py", line 1507, in fit
initial_epoch=initial_epoch)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/engine/training.py", line 1176, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/callbacks.py", line 77, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/callbacks.py", line 768, in on_epoch_end
result = self.sess.run([self.merged], feed_dict=feed_dict)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1] has negative dimensions
[[Node: output_power_sample_weights = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'output_power_sample_weights', defined at:
File "tf_keras.py", line 180, in <module>
regr = nn_model()
File "tf_keras.py", line 177, in nn_model
model.compile(optimizer = 'adam', loss ={'output_power': 'mean_squared_error', 'output_slack': 'binary_crossentropy'})
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/engine/training.py", line 870, in compile
name=name + '_sample_weights'))
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 431, in placeholder
x = tf.placeholder(dtype, shape=shape, name=name)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
name=name)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): Shape [-1] has negative dimensions
[[Node: output_power_sample_weights = Placeholder[dtype=DT_FLOAT, shape=[?], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
What does "Shape [-1] has negative dimensions" means? I had also tried with each output with callbacks.Tensorboard and there wasn't any error occurred. Also search with "Node: output_power_sample_weights" but no results.
regr.summary()
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
main_input (InputLayer) (None, 5) 0
____________________________________________________________________________________________________
Hidden (Dense) (None, 5) 30 main_input[0][0]
____________________________________________________________________________________________________
output_power (Dense) (None, 1) 6 Hidden[0][0]
____________________________________________________________________________________________________
output_slack (Dense) (None, 1) 6 Hidden[0][0]
====================================================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
The "Shape [-1] has negative dimensions" error message is a (misleading) error printed when you don't feed a tf.placeholder() that has a partially-defined shape. This error message has been replaced with a more informative message in the nightly version of TensorFlow.
The error and the stack trace suggests that this Session.run() call is failing (source):
File "/home/roy/keras_tf/local/lib/python2.7/site-packages/keras/callbacks.py", line 768, in on_epoch_end
result = self.sess.run([self.merged], feed_dict=feed_dict)
Looking at the implementation, it appears that this is only called when the TensorBoard callback is installed, and self.validation_data is not None. From the implementation, it looks like this callback is incompatible with models that depend on placeholders that are not one of (i) model.inputs, (ii) model.targets, or (iii) model.sample_weights. It might be best to open an issue on the Keras GitHub issue tracker about this.
Being a beginner to tensorflow and CNN I'm working on emotion recognition to understand these.
The following code works when dropout layer is removed, however results in Nan when added. I've googled around and came across solutions such as reducing learning rate etc. None has worked for me.
The net :
def cnn(self, data):
conv = tf.nn.conv2d(data, self.w_1, [1, 1, 1, 1], padding='SAME')
hidden = tf.nn.relu(conv + self.b_1)
pool = tf.nn.max_pool(hidden, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
norm = tf.nn.lrn(pool, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
conv = tf.nn.conv2d(norm, self.w_2, [1, 1, 1, 1], padding='SAME')
hidden = tf.nn.relu(conv + self.b_2)
pool = tf.nn.max_pool(hidden, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
norm = tf.nn.lrn(pool, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
list_shape = norm.get_shape().as_list()
reshape = tf.reshape(pool, [list_shape[0], list_shape[1] * list_shape[2] * list_shape[3]])
hidden = tf.nn.relu(tf.matmul(reshape, self.w_3) + self.b_3)
hidden = tf.nn.relu(tf.matmul(hidden, self.w_4) + self.b_4)
dropout = tf.nn.dropout(hidden, self.dropout_prob)
return tf.matmul(dropout, self.w_5) + self.b_5
The model:
self.tf_x = tf.placeholder(tf.float32, shape=(self.batch_size, self.image_size, self.image_size, 1))
self.tf_y = tf.placeholder(tf.float32, shape=(self.batch_size, self.num_labels))
self.dropout_prob = tf.placeholder(tf.float32)
self.w_1 = tf.Variable(tf.truncated_normal([5, 5, 1, 64], stddev=0.1))
self.b_1 = tf.Variable(tf.zeros([64]))
self.w_2 = tf.Variable(tf.truncated_normal([9, 9, 64, 128], stddev=0.04))
self.b_2 = tf.Variable(tf.constant(1.0, shape=[128]))
self.w_3 = tf.Variable(tf.truncated_normal([self.image_size//4 * self.image_size//4 * 128, 392], stddev=0.1))
self.b_3 = tf.Variable(tf.constant(1.0, shape=(392,)))
self.w_4 = tf.Variable(tf.truncated_normal([392, 196], stddev=0.1))
self.b_4 = tf.Variable(tf.constant(1.0, shape=(196,)))
self.w_5 = tf.Variable(tf.truncated_normal([196, self.num_labels], stddev=0.04))
self.b_5 = tf.Variable(tf.constant(1.0, shape=[self.num_labels]))
self.logits = self.cnn(self.tf_x)
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.tf_y, logits=self.logits))
self.optimizer = tf.train.AdamOptimizer(1e-6).minimize(self.loss)
self.train_pred = tf.nn.softmax(self.logits)
tf.summary.histogram('weights_1', self.w_1)
tf.summary.histogram('weights_2', self.w_2)
tf.summary.histogram('weights_3', self.w_3)
tf.summary.histogram('weights_4', self.w_4)
tf.summary.scalar('loss', self.loss)
self.merged = tf.summary.merge_all()
The error:
Traceback (most recent call last):
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 75, in <module>
main()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 64, in main
emotion_cnn.train_test_validate()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 127, in train_test_validate
_,summary, l1, predictions1 = self.session.run([self.optimizer, self.merged, self.loss, self.train_pred], feed_dict=feed_dict1)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Nan in summary histogram for: weights_1
[[Node: weights_1 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](weights_1/tag, Variable/read/_81)]]
Caused by op 'weights_1', defined at:
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 75, in <module>
main()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\main.py", line 64, in main
emotion_cnn.train_test_validate()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 104, in train_test_validate
self.model()
File "C:\Users\joyte\Documents\GitHub\Emotion-recognizer\Emotion.py", line 82, in model
tf.summary.histogram('weights_1', self.w_1)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\summary\summary.py", line 203, in histogram
tag=scope.rstrip('/'), values=values, name=scope)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 139, in _histogram_summary
name=name)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
op_def=op_def)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\joyte\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\framework\ops.py", line 1226, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): Nan in summary histogram for: weights_1
[[Node: weights_1 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](weights_1/tag, Variable/read/_81)]]
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\cuda\cuda_gpu_executor.cc:637] Deallocating stream with pending work
I was following the standard cifar 10 keras tutorial here: https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py
Which I modified to use my own training images. Each image replicates the dimensions of the cifar set, ie, they are each 32x32 and 3 channels.
Shape of each image:
(32,32,3)
However, I run into a ValueError as shown in the full output below.
X_train shape: (7200, 32, 32, 3)
7200 train samples
800 test samples
Using real-time data augmentation.
Epoch 1/200
Traceback (most recent call last):
File "<ipython-input-16-70ca8831e139>", line 162, in <module>
validation_data=(X_test, Y_test))
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/keras/models.py", line 651, in fit_generator
max_q_size=max_q_size)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 1383, in fit_generator
class_weight=class_weight)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 1167, in train_on_batch
outputs = self.train_function(ins)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 659, in __call__
updated = session.run(self.outputs + self.updates, feed_dict=feed_dict)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
File "/storage/programfiles/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 625, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (32, 32, 32, 3) for Tensor 'convolution2d_input_7:0', which has shape '(?, 3, 32, 32)'
Can anyone help me out? :)
EDIT:
I tried reshaping as follows:
X_train = X_train.reshape((7200,3,32,32))
X_test = X_test.reshape((-1,3,32,32))
It crashed instead.
You actually need to transpose your array to the correct ordering, not a reshape:
X_train = np.transpose(X_train, (0, 3, 1, 2))
X_test = np.transpose(X_test, (0, 3, 1, 2))