0

I am getting acquainted with Tensorflow keras in Python.

I try to train a very simple network, with a simple dataset created by myself. I am trying to follow the lines of the tutorial in the official tf website:

https://www.tensorflow.org/tutorials/keras/basic_regression

In particular, I have the following code:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow import layers

# Generate data
NumElems = 1000
NumDims = 2
TrainSize = int(0.6 * NumElems)
print(TrainSize)

x = np.random.rand(NumDims,NumElems)*2 - 1
y = sum(x**2)

x_training = x[:, :TrainSize]
y_training = y[:TrainSize]

x_test = x[:, TrainSize:]
y_test = y[TrainSize:]

# Build the model
NH1 = 10 #Number of hidden nodes on first layer
NH2 = 10  #Number of hidden nodes on second layer
model = keras.Sequential()
model.add(layers.Dense(NH1, activation='relu'))
model.add(layers.Dense(NH2, activation='relu'))
model.add(layers.Dense(1))

#Compile the model
optimizer = tf.keras.optimizers.RMSprop(0.001) 
model.compile(loss='mse', optimizer=optimizer, metrics=['mae', 'mse'])

#Train the model
model.fit(x_training, y_training, epochs=10, batch_size = 50)

It works well excepted for the last line, which generates the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/giuseppe/TF_Regression.py", line 38, in <module>
    model.fit(x_training, y_training, epochs=10, batch_size = 50)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
    validation_split=validation_split)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1032, in _standardize_weights
    self._set_inputs(x)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1242, in _set_inputs
    self.build(input_shape=input_shape)
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 221, in build
    with ops.name_scope(layer._name_scope()):
  File "/home/giuseppe/venv/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 151, in _name_scope
    return self._current_scope.original_name_scope
AttributeError: 'NoneType' object has no attribute 'original_name_scope'

I have no clue what it is about and how to fix it. Could someone please help me?

I thank you in advance. My best regards, Giuseppe

Giuseppe
  • 163
  • 1
  • 9

1 Answers1

0

Probably some object you are working with is None (It's probably related to x_training or y_training). Read this: Don't understand what this AttributeError means.

Jorge Ribeiro
  • 56
  • 1
  • 5
  • I have just checked that out. x_training and y_traiing are nothing else than plain numpy arrays, no none there. How could I figure out what is going wrong? (sorry , I am new to python too) – Giuseppe Feb 08 '19 at 11:59
  • You seem to have everything installed correctly. This error is related to the way you are generating your own dataset. I assume you are learning and wanna see the network learning. Why don't you try with the MNIST or CIFAR10 provided by Keras? You just have to call a function and the dataset will be downloaded, and then give it to the compiled model. See [https://keras.io/datasets/] – Jorge Ribeiro Feb 08 '19 at 13:04
  • Thank you for your comment. Indeed, I have tried out the example provided in the TensorFlow tutorial, in which they use the mnist dataset, and it works well for me. Then I try to do the same with some simple dataset generated by my own (which is what I will need in real applications), and it does not work anymore. Also, when I inspect the examples provided in the tutorial, the dataset generated there are just numpy arrays. And so do I in my simple example. – Giuseppe Feb 08 '19 at 13:13