Error in using tf.Variable for 1 dimensional tensor in tensorflow

Issue

I am trying to make a deep neural network model on Tensorflow. The tf.Variable is not working with 1 dimensional shape tensor but works with 2 dimensional shape tensor.

b_init = tf.random_normal_initializer()
print(b_init)
bias = tf.Variable(initial_value=b_init(shape=(2)),trainable=True)
bias

Error:

InvalidArgumentError: shape must be a vector of {int32,int64}, got shape [] [Op:RandomStandardNormal]

2 dim shape tensor:

b_init = tf.random_normal_initializer()
print(b_init)
bias = tf.Variable(initial_value=b_init(shape=(2,2)),trainable=True)
bias

Result:

<tensorflow.python.ops.init_ops_v2.RandomNormal object at 0x7f7cc9505d60>
Out[214]:
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[ 0.00792366, -0.00770738],
       [-0.03002863, -0.01031866]], dtype=float32)>

Solution

Your 1D input shape still needs a comma:

import tensorflow as tf


b_init = tf.random_normal_initializer()
print(b_init)
bias = tf.Variable(initial_value=b_init(shape=(2, )),trainable=True)
bias

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published