Search code examples
pythonpandasnumpyneural-networktensorflow2.0

Problems with numpy in Tensorflow


from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np

data = keras.datasets.boston_housing

(x_train, x_test), (y_train, y_test) = data.load_data()

model = keras.Sequential([
    keras.layers.InputLayer(13),
    keras.layers.Dense(3, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics="accuracy")

model.fit(x_train, y_train, epochs=10)

predict = model.predict(x_test)

for i in range(10):
    plt.grid(False)
    plt.imshow(x_test[i], cmap=plt.cm.binary)
    plt.suptitle("Actual: " + y_test[i])
    plt.title("Prediction: " + np.argmax(predict[i]))
    plt.show()`

That is my code and I need help.

I expect the normal thing that some graphs show up and it says tht everything has worked finde. But it does not. Error code:

Traceback (most recent call last):
  File "C:\Users\name\PycharmProjects\Neural network\first_self_approach.py", line 21, in <module>
    model.fit(x_train, y_train, epochs=10)

  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\data_adapter.py", line 1859, in _get_tensor_types
    return (tf.Tensor, np.ndarray, pd.Series, pd.DataFrame)

AttributeError: module 'pandas' has no attribute 'Series'

Solution

  • Meh, there are many mistakes here.

    1. this is not a classification problem, it's a regression problem.
    2. the order to load the data is wrong.
    3. How can you plot your data with imshow? you don't even have images.

    Here I give you a working example:

    from tensorflow import keras 
    import matplotlib.pyplot as plt 
    import numpy as np
    
    data = keras.datasets.boston_housing
    
    (x_train, y_train), (x_test, y_test) = data.load_data()
    
    model = keras.Sequential([
        keras.layers.Dense(3, activation="relu", input_shape=(13,)),
        keras.layers.Dense(1)
    ])
    
    model.compile(optimizer="adam", loss="mse")
    
    model.summary()
    
    model.fit(x_train, y_train, epochs=20)
    
    predict = model.predict(x_test)
    plt.scatter(y_test, predict)
    plt.show()
    

    There still a lot of things you can do to improve this model