Error converting shape to a TensorShape: only integer scalar arrays can be converted to a scalar index

Viewed 100

I ran into a problem when I tried to run the following code

import sklearn
from sklearn.datasets  import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

cal_housing = fetch_california_housing()

X_train_full,X_test, y_train_full,y_test = train_test_split(cal_housing.data,cal_housing.target)
X_train,X_valid,y_train,y_valid = train_test_split(X_train_full,y_train_full)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_valid_scaled = scaler.transform(X_valid)
X_test_scaled = scaler.transform(X_test)

input1 = keras.layers.Input(shape=X_train_scaled[1:])
hidden1 = keras.layers.Dense(30,activation="relu")(input1)
hidden2 = keras.layers.Dense(30,activation="relu")(hidden)
concat = keras.layers.concatenate((input1,hidden2))
output = keras.layers.Dense(1)(concat)
model = keras.models.Model(inputs=[input1],outputs=[output])

Error:

Error converting shape to a TensorShape: only integer scalar arrays can be converted to a scalar index.

The same code is mentioned in the book Hands-on-Machine-Learning-with-Scikit-Learn-Keras- Tensorflow

Any suggestion where it may be a problem?

1 Answers

The Input layer takes the shape argument, you are trying to pass the values of X_train_scaled instead of the shape of the input data.

Kindly, change the following line

input1 = keras.layers.Input(shape=X_train_scaled[1:])

with the line below

input1 = keras.layers.Input(shape=X_train_scaled[0].shape)

Please find the gist here. Thank you!

Related