"undefined input shape at index" warning in training - tensorflow2.0

Tensorflow2 is used in training and I have quite a number of warnings printed out in object classification training.
What could be the reason for those warnings?
2020-04-17 12:15:16.091784: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 486 in the outer inference context.
2020-04-17 12:15:16.091846: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 414 in the outer inference context.
2020-04-17 12:15:16.091860: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 529 in the outer inference context.
2020-04-17 12:15:16.091882: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 351 in the outer inference context.
2020-04-17 12:15:16.091926: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 476 in the outer inference context.
2020-04-17 12:15:16.091937: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 444 in the outer inference context.
2020-04-17 12:15:16.091953: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 372 in the outer inference context.
2020-04-17 12:15:16.091994: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 466 in the outer inference context.
2020-04-17 12:15:16.092009: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 404 in the outer inference context.
2020-04-17 12:15:16.092015: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 395 in the outer inference context.
2020-04-17 12:15:16.092022: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 341 in the outer inference context.
2020-04-17 12:15:16.092036: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 289 in the outer inference context.
2020-04-17 12:15:16.092060: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 319 in the outer inference context.
2020-04-17 12:15:16.092076: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 249 in the outer inference context.
2020-04-17 12:15:16.092087: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 219 in the outer inference context.
2020-04-17 12:15:16.092107: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 279 in the outer inference context.
2020-04-17 12:15:16.092119: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 240 in the outer inference context.
2020-04-17 12:15:16.092136: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 309 in the outer inference context.
2020-04-17 12:15:16.092160: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 210 in the outer inference context.
2020-04-17 12:15:16.092177: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 270 in the outer inference context.
2020-04-17 12:15:16.092193: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 330 in the outer inference context.
2020-04-17 12:15:16.092203: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 300 in the outer inference context.
2020-04-17 12:15:16.092238: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 229 in the outer inference context.
2020-04-17 12:15:16.092248: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 199 in the outer inference context.
2020-04-17 12:15:16.092262: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 259 in the outer inference context.
2020-04-17 12:15:16.092281: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 176 in the outer inference context.
2020-04-17 12:15:16.092291: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 156 in the outer inference context.
2020-04-17 12:15:16.092301: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 124 in the outer inference context.
2020-04-17 12:15:16.092325: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 115 in the outer inference context.
2020-04-17 12:15:16.092339: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 384 in the outer inference context.
2020-04-17 12:15:16.092353: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 361 in the outer inference context.
2020-04-17 12:15:16.092387: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 166 in the outer inference context.
2020-04-17 12:15:16.092396: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 145 in the outer inference context.
2020-04-17 12:15:16.092444: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 104 in the outer inference context.
2020-04-17 12:15:16.092454: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 134 in the outer inference context.
2020-04-17 12:15:16.092464: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 187 in the outer inference context.
2020-04-17 12:15:16.092516: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 550 in the outer inference context.
2020-04-17 12:15:16.092529: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 581 in the outer inference context.
2020-04-17 12:15:16.092548: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 520 in the outer inference context.
2020-04-17 12:15:16.092576: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 425 in the outer inference context.
2020-04-17 12:15:16.092604: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 497 in the outer inference context.
2020-04-17 12:15:16.092627: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 571 in the outer inference context.
2020-04-17 12:15:16.092639: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 539 in the outer inference context.
2020-04-17 12:15:16.092666: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 434 in the outer inference context.
2020-04-17 12:15:16.092674: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 94 in the outer inference context.
2020-04-17 12:15:16.092712: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 85 in the outer inference context.
2020-04-17 12:15:16.092840: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 455 in the outer inference context.
2020-04-17 12:15:16.092862: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 561 in the outer inference context.
2020-04-17 12:15:16.092881: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 509 in the outer inference context.
2020-04-17 12:15:16.313307: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 156 in the outer inference context.
2020-04-17 12:15:16.313353: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 134 in the outer inference context.
2020-04-17 12:15:16.313373: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 571 in the outer inference context.
2020-04-17 12:15:16.313391: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 561 in the outer inference context.
2020-04-17 12:15:16.313415: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 539 in the outer inference context.
2020-04-17 12:15:16.313448: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 414 in the outer inference context.
2020-04-17 12:15:16.313463: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 529 in the outer inference context.
2020-04-17 12:15:16.313471: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 509 in the outer inference context.
2020-04-17 12:15:16.313489: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 550 in the outer inference context.
2020-04-17 12:15:16.313495: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 94 in the outer inference context.
2020-04-17 12:15:16.313521: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 581 in the outer inference context.
2020-04-17 12:15:16.313546: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 85 in the outer inference context.
2020-04-17 12:15:16.313643: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 444 in the outer inference context.
2020-04-17 12:15:16.313655: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 404 in the outer inference context.
2020-04-17 12:15:16.313672: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 520 in the outer inference context.
2020-04-17 12:15:16.313684: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 476 in the outer inference context.
2020-04-17 12:15:16.313721: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 434 in the outer inference context.
2020-04-17 12:15:16.313732: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 395 in the outer inference context.
2020-04-17 12:15:16.313741: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 372 in the outer inference context.
2020-04-17 12:15:16.313754: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 497 in the outer inference context.
2020-04-17 12:15:16.313764: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 466 in the outer inference context.
2020-04-17 12:15:16.313799: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 341 in the outer inference context.
2020-04-17 12:15:16.313827: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 425 in the outer inference context.
2020-04-17 12:15:16.313850: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 361 in the outer inference context.
2020-04-17 12:15:16.313861: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 486 in the outer inference context.
2020-04-17 12:15:16.313873: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 455 in the outer inference context.
2020-04-17 12:15:16.313925: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 124 in the outer inference context.
2020-04-17 12:15:16.313957: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 145 in the outer inference context.
2020-04-17 12:15:16.313976: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 229 in the outer inference context.
2020-04-17 12:15:16.313985: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 351 in the outer inference context.
2020-04-17 12:15:16.313994: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 330 in the outer inference context.
2020-04-17 12:15:16.314011: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 270 in the outer inference context.
2020-04-17 12:15:16.314017: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 384 in the outer inference context.
2020-04-17 12:15:16.314030: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 219 in the outer inference context.
2020-04-17 12:15:16.314061: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 309 in the outer inference context.
2020-04-17 12:15:16.314068: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 300 in the outer inference context.
2020-04-17 12:15:16.314085: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 104 in the outer inference context.
2020-04-17 12:15:16.314139: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 210 in the outer inference context.
2020-04-17 12:15:16.314172: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 289 in the outer inference context.
2020-04-17 12:15:16.314183: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 259 in the outer inference context.
2020-04-17 12:15:16.314213: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 319 in the outer inference context.
2020-04-17 12:15:16.314234: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 249 in the outer inference context.
2020-04-17 12:15:16.314248: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 115 in the outer inference context.
2020-04-17 12:15:16.314272: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 199 in the outer inference context.
2020-04-17 12:15:16.314282: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 176 in the outer inference context.
2020-04-17 12:15:16.314289: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 240 in the outer inference context.
2020-04-17 12:15:16.314313: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 279 in the outer inference context.
2020-04-17 12:15:16.314325: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 187 in the outer inference context.
2020-04-17 12:15:16.314357: W tensorflow/core/common_runtime/shape_refiner.cc:88] Function instantiation has undefined input shape at index: 166 in the outer inference context.

I had similar errors popping up. I used a dataset from a generator without specifing the output shape of the generator. After adding the output shape, no warning was generated:
tf.data.Dataset.from_generator(generator, output_types=(tf.float32, tf.float32), output_shapes=(tf.TensorShape([2997, 16]), tf.TensorShape([None])))

Related

Serializing a tensor and writing to tfrecord from within a graph

I would like to write tensorflow example records to a TFRecordWriter from inside an AutoGraph generated graph.
The documentation for tensorflow 2.0 states the following:
The simplest way to handle non-scalar features is to use tf.serialize_tensor to convert tensors to binary-strings. Strings are scalars in tensorflow.
However, tf.io.serialize_tensor returns a tensor of byte-string. Creating an Example proto requires a bytes list, not a tensor.
How do I write a tf.train.Example to a tf record from inside a graph?
Code to reproduce:
%tensorflow_version 2.x
import tensorflow as tf
#tf.function
def example_write():
writer = tf.io.TFRecordWriter("test.tfr")
x = tf.constant([[0, 1], [2, 3]])
x = tf.io.serialize_tensor(x)
feature = {
"data": tf.train.Features(
bytes_list=tf.train.BytesList(value=[x]))
}
ex = tf.train.Example(features=tf.train.Features(
feature=feature))
writer.write(ex.SerializeToString())
example_write()
and the error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-df8a97eb17c9> in <module>()
12 writer.write(ex.SerializeToString())
13
---> 14 example_write()
8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
TypeError: in user code:
<ipython-input-6-df8a97eb17c9>:6 example_write *
feature = {
TypeError: <tf.Tensor 'SerializeTensor:0' shape=() dtype=string> has type Tensor, but expected one of: bytes
It's pretty straightforward:
use x = tf.io.serialize_tensor(x).numpy()

What's wrong with `tf.zeros_initializer` in `get_variable()`?

I want to train some adversarial examples using CW algorithm, and I used an example from here and a CW implementation from here. But I encountered an error about tf.zeros_initializer:
ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of
'tf.keras.initializers.*' and `shape` should be fully defined.
Edit: It seems that non-fully defined shape conflicts with using initializers. How can I fix it?
Here's a piece of code:
# ... omitted
with tf.variable_scope('model', reuse=tf.AUTO_REUSE):
# CW
_, env.adv_cw, _ = cw.cw(model, env.x)
Here's env.x:
env.x = tf.placeholder(tf.float32, (None, width, height, channels), name='x')
When I run the code, I get the error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-712c8b007d37> in <module>()
8 with tf.variable_scope('model', reuse=tf.AUTO_REUSE):
9 # CW
---> 10 _, env.adv_cw, _ = cw.cw(model, env.x)
5 frames
/content/cw.py in cw(model, x, y, eps, ord_, T, optimizer, alpha, min_prob, clip)
50 """
51 xshape = x.get_shape().as_list()
---> 52 noise = tf.get_variable('noise', shape=xshape, dtype=tf.float32,
53 initializer=tf.zeros_initializer)
54
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
1494 constraint=constraint,
1495 synchronization=synchronization,
-> 1496 aggregation=aggregation)
1497
1498
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
1237 constraint=constraint,
1238 synchronization=synchronization,
-> 1239 aggregation=aggregation)
1240
1241 def _get_partitioned_variable(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint, synchronization, aggregation)
560 constraint=constraint,
561 synchronization=synchronization,
--> 562 aggregation=aggregation)
563
564 def _get_partitioned_variable(self,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, constraint, synchronization, aggregation)
512 constraint=constraint,
513 synchronization=synchronization,
--> 514 aggregation=aggregation)
515
516 synchronization, aggregation, trainable = (
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource, constraint, synchronization, aggregation)
906 variable_dtype = None
907 else:
--> 908 raise ValueError("The initializer passed is not valid. It should "
909 "be a callable with no arguments and the "
910 "shape should not be provided or an instance of "
ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of `tf.keras.initializers.*' and `shape` should be fully defined.
But Google's TensorFlow Guide gives an example of the usage of get_variable:
my_int_variable = tf.get_variable("my_int_variable", [1, 2, 3], dtype=tf.int32,
initializer=tf.zeros_initializer)
Environment: Google Colab, TensorFlow 1.14.0-rc1, Python 3.6
Just make changes according to your placeholder dimension, let me take example of your placeholder variable.
** x = placeholder(t f. float 32, (None, width, height, channels), name='x')**.
It has 4 dimension : [None, width, height, channels], but width, height, channels is not defined, means, for an image width =6, height=6, channels=3 is defined so the tensor dimension is [6 x 6 x 3].
What you can do is, the image you read, get the all three dimension value in different variable and pass it to your placeholder variable.
Ex.
Image A = 32 x 32 x 3
width = A.shape[0]
height = A.shape[1]
channels =A.shape[2]
Or you can directly provide the value to width, height, channels (If you know your input data shape) by this way the placeholder will be defined.

TensorFlow Variable Shape assign

I am trying to create a variable and then trying to assign it with the value of my convolution layer.
However it is refusing because it is saying shapes are not equal even though I have passed validate_shape=False while creating the variable.
The convolution shape is [32,20,20,3]. How do I pass this into the variable?
the bottom code:
conv = tf.layers.conv2d_transpose(conv, filters=3, kernel_size=3, strides=(2,2), padding='same',activation=tf.nn.relu) # TO ASSIGN LATER
g=tf.Variable(([32,20,20]),dtype=tf.float32,validate_shape=False)#THE VARIABLE
loss = tf.reduce_mean(tf.square(conv))
opt = tf.train.AdamOptimizer().minimize(loss)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
_, xx,inp,output,target = sess.run([opt, loss,x,conv,y])#
print(xx)
print("subtraction result:",output[0]-target[0])
g=g.assign(conv)
print(g.eval())
I am getting this error:
InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [3] rhs shape= [32,20,20,3]
[[Node: Assign_7 = Assign[T=DT_FLOAT, use_locking=false, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_9, conv2d_transpose_98/Relu)]]
Can someone please help fix this?
I think you want:
import numpy as np
import tensorflow as tf
g = tf.Variable(initial_value=np.zeros((32,20,20,3)), expected_shape=(32,20,20,3), dtype=tf.float32)
If you print g you get the correct shape now:
<tf.Variable 'Variable_3:0' shape=(32, 20, 20, 3) dtype=float32_ref>
What you did was this:
g = tf.Variable(initial_value=(32,20,20), dtype=tf.float32, valid_shape=False)
By not stating expected_shape you defaulted to positional arguments, the first argument of tf.Variable is initial_value as per the documentation below:
__init__(
initial_value=None,
trainable=True,
collections=None,
validate_shape=True,
caching_device=None,
name=None,
variable_def=None,
dtype=None,
expected_shape=None,
import_scope=None,
constraint=None
)
That shape of the initial_value you declared would have been a vector of length [3] which is exactly the shape that the assign operation is complaining about.
Moral of the story: it's generally less buggy to declare arguments by name if you can. :)

tf.train.batch( allow_smaller_final_batch=True, ) does not shape batch correctly

If I use tf.train.batch( allow_smaller_final_batch=True, ) the shape of the tensor is unknown:
allow_smaller_final_batch=True: Tensor shape= (?, 224, 224, 3)
allow_smaller_final_batch=False: Tensor shape= (16, 224, 224, 3)
This is giving me an error downstream.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/anaconda/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
467 try:
--> 468 str_values = [compat.as_bytes(x) for x in proto_values]
469 except TypeError:
/anaconda/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in <listcomp>(.0)
467 try:
--> 468 str_values = [compat.as_bytes(x) for x in proto_values]
469 except TypeError:
/anaconda/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/compat.py in as_bytes(bytes_or_text, encoding)
64 raise TypeError('Expected binary or unicode string, got %r' %
---> 65 (bytes_or_text,))
66
TypeError: Expected binary or unicode string, got None
...
TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [None, 1]. Consider casting elements to a supported type.
How do I get the batch_size before I evaluate in session?
batch_size is a hyper-parameter that you should assign to it, rather than a value to be evaluated in TensorFlow session, while there's not enough information to determine what trouble you encounter.

Setting the shape of a tensor as the shape of another tensor

I'm trying to run this piece of code:
def somefunc(x, rows, n_hidden):
vectors = tf.contrib.layers.embed_sequence(nodes, vocab_size=vocab_size, embed_dim=n_hidden)
batch_size = tf.shape(vectors)[0]
state = tf.zeros([batch_size, rows, n_hidden])
bias = tf.Variable(tf.constant(0.1, shape=[batch_size,1]) # Error here!
...
x = tf.placeholder(tf.int32, shape=[None, 200])
pred = somefunc(x, 200, 40)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=target))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
I get this error when the function is called (error is for bias shape):
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
I tried doing b = tf.Variable(0.1, validate_shape=False), but then I got this error at optimizer:
ValueError: as_list() is not defined on an unknown TensorShape.
If I remove validate_shape=False, I get a shape error.
I'm very sorry if I'm overlooking something obvious, but could someone tell me where I'm going wrong?
Thank you very much!
The shape argument of the tf.constant() op expects a static shape, so you can't use a tf.Tensor as part of the argument.
Fortunately there is another op that will suffice: tf.fill(), which allows the shape (its dims argument) to be a tf.Tensor. This means you can define bias as:
bias = tf.Variable(tf.fill(dims=[batch_size, 1], 0.1), validate_shape=False)