I built a 4-layer feed forward network. However the prediction for the test sample are all the same.They are a fixed number. I have standardized the data into [-1,1]. I am not sure if there is something wrong with my code.
my data google drive: https://drive.google.com/drive/folders/1nC48KTEftjYPjYBu5h43cHQt3jbSwhQA
python code are as follows:
# import and function
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.regularizers import l1
def R_oos_tf(y_true, y_pred):
resid = tf.square(y_true-y_pred)
denom = tf.square(y_true)
return 1 - tf.divide(tf.reduce_mean(resid),tf.reduce_mean(denom))
# read data
X_trn = pd.read_pickle("X_trn.pkl")
X_vld = pd.read_pickle("X_vld.pkl")
X_tst = pd.read_pickle("X_tst.pkl")
y_trn = pd.read_pickle("y_trn.pkl")
y_vld = pd.read_pickle("y_vld.pkl")
y_tst = pd.read_pickle("y_tst.pkl")
# features
features = list(set(X_trn.columns).difference({'permno','DATE','ret_exc_lead1m','ret','ret_exc'}))
# feed forward network- 4layer
mod = Sequential()
mod.add(Input(shape=(X_trn[features].shape[1],)))
mod.add(Dense(32,activation='relu', kernel_regularizer=l1(0.01)))
mod.add(Dense(16,activation='relu', kernel_regularizer=l1(0.01)))
mod.add(Dense(8 ,activation='relu', kernel_regularizer=l1(0.01)))
mod.add(Dense(4 ,activation='relu', kernel_regularizer=l1(0.01)))
mod.add(Dense(1))
mod.compile(optimizer='adam', loss='mse', metrics=[R_oos_tf])
mod.fit(X_trn[features], y_trn, epochs=10, batch_size=200, validation_data=(X_vld[features], y_vld))
nn4_pre = pd.DataFrame(mod.predict(X_tst[features]),columns=["nn4"])
# all the prediction is a fixed number!!??
nn4_pre.nn4.value_counts()
# result 0.007084 2911
I want to know what happened and why my prediction for over 2000 rows are a fixed number.
New contributor
Jinlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.