0

I am trying to program a small regression neural network as a starting point to learn the basics.

This is the simple data set that I am using:

https://i.sstatic.net/xx6mm.png

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import pandas as pd
import io
import os
import requests
import numpy as np
from sklearn import metrics

df = pd.read_csv("C:\\Users\\Dan\\y_sinx.csv")

x = df['x'].values #Pandas to Numpy
y = df['y'].values


print(type(x)) #check type
print(np.shape(x)) #check dimensions
print(x) #check x

#Network
model = Sequential()
model.add(Dense(7, input_shape = x.shape, activation='relu')) #Hidden layer 1
model.add(Dense(4, activation='relu')) #Hidden layer 2
model.add(Dense(1)) #Output layer
model.compile(loss='mean_squared_error', optimizer = 'adam')
model.fit(x, y, verbose = 2, epochs = 20)

This code gives the outputs:

<class 'numpy.ndarray'>
(7,)
[0.         0.78539816 1.57079633 2.35619449 3.14159265 3.92699082
 4.71238898]

So it appears to be the correct size (7,), but perhaps the output of x itself looks wrong, it should be one column? I get the error:

ValueError                                Traceback (most recent call last)
<ipython-input-1-5db977397f3e> in <module>
     24 model.add(Dense(1)) #Output layer
     25 model.compile(loss='mean_squared_error', optimizer = 'adam')
---> 26 model.fit(x, y, verbose = 2, epochs = 20)
     27 
     28 #Prediction

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    641         max_queue_size=max_queue_size,
    642         workers=workers,
--> 643         use_multiprocessing=use_multiprocessing)
    644 
    645   def evaluate(self,

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
    630         steps=steps_per_epoch,
    631         validation_split=validation_split,
--> 632         shuffle=shuffle)
    633 
    634     if validation_data:

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2426           feed_input_shapes,
   2427           check_batch_axis=False,  # Don't enforce the batch size.
-> 2428           exception_prefix='input')
   2429 
   2430     if y is not None:

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    519                              ': expected ' + names[i] + ' to have shape ' +
    520                              str(shape) + ' but got array with shape ' +
--> 521                              str(data_shape))
    522   return data
    523 

ValueError: Error when checking input: expected dense_input to have shape (7,) but got array with shape (1,)

I'm not sure how it got an array with shape (1,) and how to fix it, help would be greatly appreciated!

1 Answer 1

1

Keras is expecting the number of attributes or variables of X in the input layer, but you defined your input layer as

model.add(Dense(7, input_shape = x.shape, activation='relu')) #Hidden layer 1

So, that means that there will be 7 hidden units in the input layer, which should not be true, because you only have 1 variable in X. Try doing:

model.add(Dense(1, input_dim = x.shape[0], activation='relu')) #Input layer
Sign up to request clarification or add additional context in comments.

2 Comments

When I tried that I got: ValueError: Error when checking input: expected dense_32_input to have shape (7,) but got array with shape (1,)
Yes, sorry. Delete the input_dim argument and there will be no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.