我是 Keras 的新手,在形状方面遇到了一些问题,特别是涉及到 RNN 和 LSTM 时。
我正在运行这段代码:
model.add(SimpleRNN(init='uniform',output_dim=1,input_dim=len(pred_frame.columns)))
model.compile(loss="mse", optimizer="sgd")
model.fit(X=predictor_train, y=target_train, batch_size=len(pred_frame.index),show_accuracy=True)
变量 predictor_train 是一个带有 119 个内部数组的 numpy 数组,每个数组有 80 个不同的项目。
我遇到了这个错误:
TypeError: ('Bad input argument to theano function with name "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py:362" at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (119, 80).')
到目前为止,我发现 RNN 接收形状为 (batch_size, timesteps, dimension) 的 3D 张量,当您设置 input_shape 时,通常会省略 batch_size,您应该只提供一个 (timesteps, dimension) 的元组.但是应该更改代码的哪一部分(如果可能,请添加您的代码更改建议)?
About pred_frame
类型:类'pandas.core.frame.DataFrame'
形状:(206,80)
Pred Pred Pred ...
Date
1999-01-01 NaN NaN NaN
1999-02-01 NaN NaN NaN
1999-03-01 NaN NaN NaN
1999-04-01 NaN NaN NaN
...
2015-11-01 288.333333 -0.044705 589.866667
2015-12-01 276.333333 -0.032157 1175.466667
2016-01-01 282.166667 0.043900 1458.966667
2016-02-01 248.833333 -0.082199 5018.966667
[206 rows x 80 columns]
About target_train
类型:类'pandas.core.series.Series'
形状:(119,)
数据类型:float64
Date
2004-10-01 0.003701
2005-05-01 0.001715
2005-06-01 0.002031
2005-07-01 0.002818
...
2015-05-01 -0.007597
2015-06-01 -0.007597
2015-07-01 -0.007597
2015-08-01 -0.007597
About predictor_train
类型:'numpy.ndarray'
形状:(119,80)
数据类型:float64
[[ 0.00000000e+00 -1.00000000e+00 1.03550000e-02 ..., 8.42105263e-01
6.50000000e+01 -3.98148148e-01]
[ -1.13600000e-02 -1.07482052e+00 -9.25333333e-03 ..., 4.45783133e-01
8.30000000e+01 -1.94915254e-01]
[ 4.71300000e-02 -5.14876761e+00 1.63166667e-03 ..., 4.45783133e-01
8.50000000e+01 -1.94915254e-01]
...,
[ 4.73500000e-02 -1.81092653e+00 -8.54000000e-03 ..., 1.39772727e+00
2.77000000e+02 -3.43601896e-01]
[ -6.46000000e-03 -1.13643083e+00 1.06100000e-02 ..., 2.22551929e-01
2.77000000e+02 -3.43601896e-01]
[ 3.14200000e-02 -5.86377709e+00 1.50850000e-02 ..., 2.22551929e-01
2.82000000e+02 -2.76699029e-01]]
感谢@y300,显然 3d 问题已经解决了。我现在的形状是 (119,1,80)。
model.summary() returns the following:
--------------------------------------------------------------------------------
Initial input shape: (None, None, 119)
--------------------------------------------------------------------------------
Layer (name) Output Shape Param #
--------------------------------------------------------------------------------
SimpleRNN (Unnamed) (None, 1) 121
Total params: 121
但是,我仍然在 model.fit 行中遇到整形问题,如下所示:
File "/Library/Python/2.7/site-packages/theano/tensor/blas.py", line 1612, in perform
z[0] = numpy.asarray(numpy.dot(x, y))
ValueError: ('shapes (119,80) and (119,1) not aligned: 80 (dim 1) != 119 (dim 0)', (119, 80), (119, 1))
Apply node that caused the error: Dot22(Alloc.0, <TensorType(float32, matrix)>)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(119, 80), (119, 1)]
Inputs strides: [(320, 4), (4, 4)]
Inputs values: ['not shown', 'not shown']
为什么会发生这种情况,我该如何解决?
最佳答案
你可以检查你的模型是什么样子的
model.summary()
在这种情况下,您应该看起来像这样(实际值可能不同):
--------------------------------------------------------------------------------
Initial input shape: (None, None, 100)
--------------------------------------------------------------------------------
Layer (name) Output Shape Param #
--------------------------------------------------------------------------------
SimpleRNN (simplernn) (None, 1) 102
--------------------------------------------------------------------------------
Total params: 102
--------------------------------------------------------------------------------
如您所见,输入是 3D 张量,而不是 2D 张量。因此,您需要 reshape 阵列以适应 keras 的预期。特别是,输入 X_train 应具有维度 (num_samples,1,input_dim)。这是一个使用一些随机生成的 x/y 数据的工作示例:
model.add(keras.layers.SimpleRNN(init='uniform',output_dim=1,input_dim=100))
model.compile(loss="mse", optimizer="sgd")
X_train = np.random.rand(300,1,100)
y_train = np.random.rand(300)
model.fit(X=X_train, y=y_train, batch_size=32,show_accuracy=True)
关于python - 维数错误 : expected 3, 得到 2 形状 (119, 80),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241731/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]