Python 为什么我';当我使用model.predict时,我在keras中得到了所有的1?
我试图在keras包中使用LSTM。我将所有的训练和测试数据标准化为0到1之间的刻度。我将阵列重塑为3D阵列。但当我运行查看结果时,我得到的是1的数组,而不是1到0之间的缩放值 这是我的密码:Python 为什么我';当我使用model.predict时,我在keras中得到了所有的1?,python,keras,Python,Keras,我试图在keras包中使用LSTM。我将所有的训练和测试数据标准化为0到1之间的刻度。我将阵列重塑为3D阵列。但当我运行查看结果时,我得到的是1的数组,而不是1到0之间的缩放值 这是我的密码: Input = pd.read_csv('Input.csv') for i in range(0, len(Input['Gold Price'])): Input['Gold Price'][i] = float(Input['Gold Price'][i].replace(',',''))
Input = pd.read_csv('Input.csv')
for i in range(0, len(Input['Gold Price'])):
Input['Gold Price'][i] = float(Input['Gold Price'][i].replace(',',''))
Input['DJIA'][i] = float(Input['DJIA'][i].replace(',',''))
x_train = Input[['DJIA', 'Silver Price', 'Copper Price', 'US Dollar Index',
'VIX']]
y_train = Input[['Gold Price']]
scaler = MinMaxScaler(feature_range=(0, 0.95))
scaler_x = scaler.fit(x_train)
scaler_y = scaler.fit(y_train)
x_scaled = scaler_x.transform(x_train)
y_scaled = scaler_y.transform(y_train)
x_scaled = np.reshape(x_scaled, (x_scaled.shape[0], 1, x_scaled.shape[1]))
model = Sequential()
model.add(LSTM(5, activation='relu', input_dim=5))
model.add(Dense(units=1, activation='softmax'))
model.compile(loss='mae',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_scaled, y_scaled, epochs=5, batch_size=32)
Test = pd.read_csv('Test Prices Daily.csv')
for i in range(0, len(Test['DJIA'])):
Test['DJIA'][i] = float(Test['DJIA'][i].replace(',',''))
x_test = Test[['DJIA', 'Silver Price', 'Copper Price', 'US Dollar Index',
'VIX']]
scaler_x = scaler.fit(x_test)
x_scaled_test = scaler_x.transform(x_test)
x_scaled_test = np.reshape(x_scaled_test, (x_scaled_test.shape[0], 1,
x_scaled_test.shape[1]))
prediction = model.predict(x_scaled_test, batch_size=128)
这是一条令人不快的路线。激活应该是乙状结肠
原因是softmax由
softmax(x)_i=e^x_i/sum_i e^x_i
使输出向量正常化,使分量和为1(即,将其映射到单纯形)。如果组件输出为1,则它必须始终为1
我也有点惊讶地看到:
loss='mae'
并且认为二进制交叉熵是一个更好的选择,因为你的回答在0和1之间。我想这取决于将输出变量解释为函数近似值或概率,这是我所不知道的。也许只要删除这一行就行了。
scaler\u x=scaler.fit(x\u测试)
谢谢你的工作。但是,现在我所有的输出都是一样的。不管怎样,要解决这个问题吗?
loss='mae'