【问题标题】:Keras and the input layerKeras 和输入层
【发布时间】:2017-09-19 12:27:34
【问题描述】:

所以我正在尝试使用 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

【问题讨论】:

  • 首先你应该定义这是一个回归问题还是一个分类问题,要预测的目标值是什么,以及它的维度。

标签: machine-learning neural-network keras theano


【解决方案1】:

我认为您使用了错误的指标: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'])

和一个单神经元输出层。

【讨论】:

  • 输出层只是一组正常的神经元?我不需要像输入这样的特殊层吗? IE 我不需要设置输出尺寸或输出形状?我问的原因是我仍然得到奇怪的“ValueError:检查模型目标时出错:预期dense_2具有形状(50、20)但得到形状为(50、302)的数组”错误
  • 您不需要输入层,因为您已经指定了第一个密集层的 input_shape。输出层的#dimensions 应该是类别的数量。你有多少个类别?
  • 我想要输出 3 个类别
  • 但是你的输出向量总是有值:301.44!
  • 您需要应用 one-hot 编码 (en.wikipedia.org/wiki/One-hot)。如果您只是使用 np_utils.to_categorical 和 302(301.4 上限)的向量,那么它将假定您有 302 个类别。
猜你喜欢
  • 2019-11-13
  • 1970-01-01
  • 2018-09-07
  • 2016-03-24
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 2018-02-27
  • 2019-07-31
相关资源
最近更新 更多