Python:keras形状不匹配错误

时间:2015-08-13 20:00:31

标签: python keras

我正试图在model = Sequential() model.add(Dense(16, 8, init='uniform', activation='tanh')) model.add(Dropout(0.5)) model.add(Dense(8, 2, init='uniform', activation='tanh')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='mean_squared_error', optimizer=sgd) model.fit(X_train, y_train, nb_epoch=1000, batch_size=50) score = model.evaluate(X_test, y_test, batch_size=50) 中构建一个非常简单的多层感知器(MLP):

X_train.shape

我的训练数据形状:(34180, 16)给出了y_train.shape

标签属于具有形状的二进制类:(34180,)给出keras

因此,我的16x8 => 8x2代码应该生成具有以下连接的网络:ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1) Apply node that caused the error: Elemwise{sub,no_inplace}(Elemwise{Composite{tanh((i0 + i1))}}[(0, 0)].0, <TensorType(float64, matrix)>) Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] Inputs shapes: [(50, 2), (50, 1)] Inputs strides: [(16, 8), (8, 8)]

产生形状不匹配错误:

Epoch 0

model.fit(X_train, y_train, nb_epoch=1000, batch_size=50)<div ng-app="app" ng-controller="demoController"> <ion-item ng-repeat="post in data | filter:{activated:true}" ng-click="deativate(post.id)"> {{post.col1}} {{post.activated }} {{"click Me"}} </br> </ion-item> </div> var app= angular.module("app",[]); app.controller('demoController', ['$scope', function($scope) { $scope.data = [ {id:1,col1:"abc",activated:true}, {id:2,col1:"abc1",activated:true}, {id:3,col1:"abc2",activated:true}, {id:4,col1:"abc3",activated:true}, {id:5,col1:"abc4",activated:false}, {id:6,col1:"abc5",activated:true}, ]; $scope.deativate = function(id){ angular.forEach($scope.data,function(item){ if(item.id === id){ item.activated = false; } }) } }]); 。我是否在监督Keras中显而易见的事情?

编辑:我已经完成了问题here,但没有解决我的问题

1 个答案:

答案 0 :(得分:10)

我遇到了同样的问题,然后找到了这个帖子;

https://github.com/fchollet/keras/issues/68

您似乎要说明最终输出图层为2或任意数量的类别,标签需要是分类类型,其中每个观察点基本上是一个二进制向量,例如3类输出向量[0,2] ,1,0,1,0]变为[[1,0,0],[0,0,1],[0,1,0],[1,0,0],[0,1,0] ,[1,0,0]。

np_utils.to_categorical函数为我解决了这个问题;

from keras.utils import np_utils, generic_utils

y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]