mavzolej
mavzolej

Reputation: 200

AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'?

As I'm running beginner.ipynb from google's intro to tensorflow locally, the execution breaks at

predictions = model(x_train[:1]).numpy()

with the following error:


AttributeError                            Traceback (most recent call last)

<ipython-input-15-6d3178b039b4> in <module>
----> 1 predictions = model(x_train[:1]).numpy()
      2 # print( type( predictions ) )
      3 # predictions = tf.convert_to_tensor( model(x_train[:1]) )
      4 predictions
      5 

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    590       else:
    591         # Eager execution on data tensors.
--> 592         outputs = self.call(inputs, *args, **kwargs)
    593         self._handle_activity_regularization(inputs, outputs)
    594         return outputs

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py in call(self, inputs, training, mask)
    228   def call(self, inputs, training=None, mask=None):
    229     if self._is_graph_network:
--> 230       return super(Sequential, self).call(inputs, training=training, mask=mask)
    231 
    232     outputs, _ = self._call_and_compute_mask(

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in call(self, inputs, training, mask)
    813     outputs, _ = self._run_internal_graph(inputs,
    814                                           training=training,
--> 815                                           mask=masks)
    816     return outputs
    817 

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in _run_internal_graph(self, inputs, training, mask)
   1000                   output_tensors = layer(computed_tensor, **kwargs)
   1001                 else:
-> 1002                   output_tensors = layer.call(computed_tensor, **kwargs)
   1003                 if hasattr(layer, 'compute_mask'):
   1004                   output_masks = layer.compute_mask(computed_tensor,

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py in call(self, inputs)
    553                  array_ops.shape(inputs)[0], -1))
    554     if not context.executing_eagerly():
--> 555       outputs.set_shape(self.compute_output_shape(inputs.get_shape()))
    556     return outputs
    557 

AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

I found a similar question here but was unable to apply the suggested fix.

How should I make the code work? Why is the same code running fine on collab but fails on my local machine?

Upvotes: 4

Views: 3068

Answers (1)

AzyCrw4282
AzyCrw4282

Reputation: 7754

The answer, as mentioned by the OP is to to use tensorflow2. Though it wasn't possible to get to the root cause of this problem it seems like it may be stemming from unsupported functionality in tensorflow1.x.

See here for the changes of 1.x vs 2.x

Upvotes: 1

Related Questions