0
$\begingroup$

I'm trying to construct a basic neural network in TensorFlow by following an example in Hands-On Machine Learning by Aurelian. The following code

n_inputs = 4
n_hidden = 4
n_outputs = 1
initializer = tf.contrib.layers.variance_scaling_initializer()

learning_rate = 0.01

X = tf.placeholder(tf.float32, shape=[None, n_inputs])
hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu,
                         kernel_initializer=initializer)
logits = tf.layers.dense(hidden, n_outputs,
                         kernel_initializer=initializer)

outputs = tf.nn.sigmoid(logits)
p_left_and_right = tf.concat(axis=1, values=[outputs, 1-outputs])


action = tf.multinomial(tf.log(p_left_and_right), num_samples=1)
y = 1. - tf.to_float(action)
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(
    labels=y, logits=logits)
print("cross_entropy: ", cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate)
grads_and_vars = optimizer.compute_gradients(cross_entropy)
gradients = [grad for grad, variable in grads_and_vars]
gradient_placeholders = []
grads_and_vars_feed = []
print("grads_and_vars:", grads_and_vars)
for grad, variable in grads_and_vars:
  gradient_placeholder = tf.placeholder(tf.float32, shape=grad.get_shape())
  gradient_placeholders.append(gradient_placeholder)
  grads_and_vars_feed.append((gradient_placeholder, variable))
training_op = optimizer.apply_gradients(grads_and_vars_feed)

init = tf.global_variables_initializer()
saver = tf.train.Saver()

produces the error

cross_entropy:  Tensor("logistic_loss_15:0", shape=(3, 1), dtype=float32)
grads_and_vars: [(None, <tf.Variable 'dense/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_1/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_1/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_2/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_2/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_3/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_3/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_4/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_4/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_5/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_5/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_6/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_6/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_7/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_7/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_8/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_8/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_9/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_9/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_10/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_10/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_11/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_11/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_12/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_12/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_13/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_13/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_14/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_14/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_15/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_15/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_16/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_16/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_17/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_17/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_18/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_18/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_19/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_19/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_20/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_20/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_21/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_21/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_22/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_22/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_23/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_23/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_24/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_24/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_25/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_25/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_26/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_26/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_27/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_27/bias:0' shape=(1,) dtype=float32_ref>), (None, <tf.Variable 'dense_28/kernel:0' shape=(4, 4) dtype=float32_ref>), (None, <tf.Variable 'dense_28/bias:0' shape=(4,) dtype=float32_ref>), (None, <tf.Variable 'dense_29/kernel:0' shape=(4, 1) dtype=float32_ref>), (None, <tf.Variable 'dense_29/bias:0' shape=(1,) dtype=float32_ref>), (<tf.Tensor 'gradients_14/dense_30/MatMul_grad/tuple/control_dependency_1:0' shape=(4, 4) dtype=float32>, <tf.Variable 'dense_30/kernel:0' shape=(4, 4) dtype=float32_ref>), (<tf.Tensor 'gradients_14/dense_30/BiasAdd_grad/tuple/control_dependency_1:0' shape=(4,) dtype=float32>, <tf.Variable 'dense_30/bias:0' shape=(4,) dtype=float32_ref>), (<tf.Tensor 'gradients_14/dense_31/MatMul_grad/tuple/control_dependency_1:0' shape=(4, 1) dtype=float32>, <tf.Variable 'dense_31/kernel:0' shape=(4, 1) dtype=float32_ref>), (<tf.Tensor 'gradients_14/dense_31/BiasAdd_grad/tuple/control_dependency_1:0' shape=(1,) dtype=float32>, <tf.Variable 'dense_31/bias:0' shape=(1,) dtype=float32_ref>)]

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-29-c35bfbdeba85> in <module>()
     27 print("grads_and_vars:", grads_and_vars)
     28 for grad, variable in grads_and_vars:
---> 29   gradient_placeholder = tf.placeholder(tf.float32, shape=grad.get_shape())
     30   gradient_placeholders.append(gradient_placeholder)
     31   grads_and_vars_feed.append((gradient_placeholder, variable))

AttributeError: 'NoneType' object has no attribute 'get_shape'

My best guess was that this had to do with passing in None for the first dimension of shape, but changing that into a number does nothing for the error. What is the cause of this?

Also, I'm relatively new to TensorFlow and this type of neural network, so don't be afraid to dumb things down. ;D

Thanks!

$\endgroup$

1 Answer 1

0
$\begingroup$

I'm afraid to be obvious but in case you miss: it says 'grad' is None. I would create a breakpoint right after you assign "grad_and_vars" and inspect it

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.