Home > front end >  Error in using tf.Variable for 1 dimensional tensor in tensorflow
Error in using tf.Variable for 1 dimensional tensor in tensorflow

Time:07-02

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)>

CodePudding user response:

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
  • Related