0

writing a program, use keras to build BP neural networ to predict data(regression),the program is as follows:

bp_dataset = pd.read_csv('Dataset/allGlassStraightThroughTube.csv')
bp_tube_par = bp_dataset.iloc[:, 3:8]
bp_tube_eff = bp_dataset.iloc[:, -1:]


bp_tube_par_X_train,bp_tube_par_X_test,bp_tube_eff_Y_train,bp_tube_eff_Y_test = train_test_split(bp_tube_par,
                                                                                                 bp_tube_eff,
                                                                                                 random_state=33,
                                                                                                 test_size=0.3)

# normalize the train and test Dataset
sc_X = StandardScaler()
sc_Y = StandardScaler()
sc_bp_tube_par_X_train = sc_X.fit_transform(bp_tube_par_X_train)
sc_bp_tube_par_X_test = sc_X.transform(bp_tube_par_X_test)
sc_bp_tube_eff_Y_train = sc_Y.fit_transform(bp_tube_eff_Y_train)
sc_bp_tube_eff_Y_test = sc_Y.transform(bp_tube_eff_Y_test)

# build BP neural network
model = Sequential()
model.add(Dense(12, input_dim=5, activation='relu'))
model.add(Dense(12, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy', 'mae'])
model.fit(sc_bp_tube_par_X_train, sc_bp_tube_eff_Y_train, epochs=100)
pre_sc_bp_tube_eff_Y_test = model.predict(sc_bp_tube_par_X_test)

but it errors:

Traceback (most recent call last):
  File "C:/Users/win/PycharmProjects/allGlassStraightThroughTube/bpTest.py", line 44, in <module>
model.fit(sc_bp_tube_par_X_train, sc_bp_tube_eff_Y_train, epochs=100)
  ...
  ValueError: Error when checking target: expected dense_2 to have shape (12,) but got array with shape (1,)

could you please tell me the reason and how to correct it

1 Answer 1

1
model.add(Dense(12, activation='linear'))

12 here represents output dimension. In your case 12 is input dimension to second layer. Keras handle input dimensions for middle layers and you dont have to mention it explicitly.

Your code should be

model.add(Dense(1, activation='linear'))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but I modify it to (Dense(1,activation='linear')),the program can run, but when print the fit process, the "accuracy" always are zero, why
Accuracy for regression does not make much sense. MSE or MAE is a better metric.

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.