2
votes

I have written that little helloworld-kind of neural network. The problem is that I constantly get that error which says:

"Traceback (most recent call last):
  File "C:/Users/Pigeonnn/PycharmProjects/Noss/Network.py", line 21, in <module>
    model.add(keras.layers.InputLayer(input_shape))
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 442, in _method_wrapper
    method(self, *args, **kwargs)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 145, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.input_layer.InputLayer object at 0x0000015EDB394DA0>"

Here's my code:

import keras
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.utils import shuffle
import tensorflow as tf

seed = 10
np.random.seed(seed)

dataset = np.loadtxt("dataset2.csv",delimiter=',',skiprows=1)
dataset = shuffle(dataset)

X = dataset[:,2:]
Y = dataset[:,1]

(X_train,X_test,Y_train,Y_test) = train_test_split(X, Y, test_size=0.15, random_state=seed)
input_shape = (13,)

model = tf.keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape))
model.add(keras.layers.core.Dense(128, activation='relu'))
model.add(keras.layers.core.Dense(128, activation='relu'))
model.add(keras.layers.core.Dense(4, activation='sigmoid'))

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(X_train,Y_train,epochs=20)

EDIT: after some tweaks(changing loss function, removing tf model), I have another error, this time its:

Traceback (most recent call last):
  File "C:/Users/Pigeonnn/PycharmProjects/Noss/Network.py", line 28, in 
    model.fit(X_train,Y_train,epochs=20)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 952, in fit
    batch_size=batch_size)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 789, in _standardize_user_data
    exception_prefix='target')
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (4,) but got array with shape (1,)
1
I edited post with full TracebackPigeonnn
I know, I checked that thread. I don't think actually that those threads are similiar enough to help me.Pigeonnn

1 Answers

4
votes

You are using both tf.keras and keras modules, which are not compatible. Use only one and be consistent.