所以我正在尝试用Keras学习ANN,因为我听说Theano或TensorFlow更简单。我有很多问题,第一个问题是输入层。
到目前为止,我将这行代码作为输入:
model.add(Dense(3 ,input_shape=(2,), batch_size=50 ,activation='relu'))
现在我要添加到模型中的数据具有以下形状:
Index(['stock_price', 'stock_volume', 'sentiment'], dtype='object')
[[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 1.42857143e-01]
[ 3.01440000e+02 7.87830000e+04 5.88235294e-02]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 0.00000000e+00]
[ 3.01440000e+02 7.87830000e+04 5.26315789e-02]]
我想制作一个模型,看看我是否可以找到股票价格和推文情绪之间的相关性,我只是在那里投入量,因为最终,我想看看它是否也可以找到一个模式。
所以我的第二个问题是在用几个不同的参数运行我的输入层之后我得到了这个我无法解释的问题。所以,当我运行这一行时:
model.add(Dense(3 ,input_shape=(2,), batch_size=50 ,activation='relu'))
使用以下行我得到此输出错误:
ValueError: Error when checking model input: expected dense_1_input to have shape (50, 2) but got array with shape (50, 3)
但是当我将输入形状更改为请求的'3'时,我收到此错误:
ValueError: Error when checking model target: expected dense_2 to have shape (50, 1) but got array with shape (50, 302)
为什么错误信息中的2变为'302'?
我可能忽略了一些非常基本的问题,因为这是我试图实现的第一个神经网络,因为我之前只使用过Weka的应用程序。
无论如何,这里是我的完整代码的副本:
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Input
from keras.optimizers import SGD
from keras.utils import np_utils
import pymysql as mysql
import pandas as pd
import config
import numpy
import pprint
model = Sequential()
try:
sql = "SELECT stock_price, stock_volume, sentiment FROM tweets LIMIT 50"
con = mysql.connect(config.dbhost, config.dbuser, config.dbpassword, config.dbname, charset='utf8mb4', autocommit=True)
results = pd.read_sql(sql=sql, con=con, columns=['stock_price', 'stock_volume', 'sentiment'])
finally:
con.close()
npResults = results.as_matrix()
cols = np_utils.to_categorical(results['stock_price'].values)
data = results.values
print(cols)
# inputs:
# 1st = stock price
# 2nd = tweet sentiment
# 3rd = volume
model.add(Dense(3 ,input_shape=(3,), batch_size=50 ,activation='relu'))
model.add(Dense(20, activation='linear'))
sgd = SGD(lr=0.3, decay=0.01, momentum=0.2)
model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.summary()
model.fit(x=data, y=cols, epochs=100, batch_size=100, verbose=2)
编辑:
以下是我从控制台获得的所有输出:
C:\Users\Def\Anaconda3\python.exe C:/Users/Def/Dropbox/Dissertation/ann.py
Using Theano backend.
C:\Users\Def\Dropbox\Dissertation
[[ 0. 0. 0. ..., 0. 0. 1.]
[ 0. 0. 0. ..., 0. 0. 1.]
[ 0. 0. 0. ..., 0. 0. 1.]
...,
[ 0. 0. 0. ..., 0. 0. 1.]
[ 0. 0. 0. ..., 0. 0. 1.]
[ 0. 0. 0. ..., 0. 0. 1.]]
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (50, 3) 12
_________________________________________________________________
dense_2 (Dense) (50, 20) 80
=================================================================
Traceback (most recent call last):
File "C:/Users/Def/Dropbox/Dissertation/ann.py", line 38, in <module>
model.fit(x=data, y=cols, epochs=100, batch_size=100, verbose=2)
File "C:\Users\Def\Anaconda3\lib\site-packages\keras\models.py", line 845, in fit
initial_epoch=initial_epoch)
File "C:\Users\Def\Anaconda3\lib\site-packages\keras\engine\training.py", line 1405, in fit
batch_size=batch_size)
File "C:\Users\Def\Anaconda3\lib\site-packages\keras\engine\training.py", line 1299, in _standardize_user_data
exception_prefix='model target')
File "C:\Users\Def\Anaconda3\lib\site-packages\keras\engine\training.py", line 133, in _standardize_input_data
str(array.shape))
ValueError: Error when checking model target: expected dense_2 to have shape (50, 20) but got array with shape (50, 302)
Total params: 92.0
Trainable params: 92
Non-trainable params: 0.0
_________________________________________________________________
Process finished with exit code 1
答案 0 :(得分:0)
我认为您使用了错误的指标:sparse_categorical_crossentropy
您是否有理由比正常情况更喜欢这个:categorical_crossentropy
?
使用categorical_crossentropy
时,您应该在1-hot编码中对目标进行编码(例如:cols = np_utils.to_categorical(results['stock_price'].values)
)。
另一方面,sparse_categorical_crossentropy使用基于整数的标签。
所以要么使用:
cols = np_utils.to_categorical(results['stock_price'].values)
与
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
和(num-categories)神经元的输出层
或使用:
cols = results['stock_price'].values.astype(np.int32)
与
model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
和单神经元输出层。