keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60)但是有形状的数组(10,100)

时间:2018-02-19 06:09:36

标签: python tensorflow keras convolution

我正在尝试移植RocAlphaGo来玩亚马逊游戏,并且在尝试实施有监督的政策培训师时会遇到问题。

from keras.models import Sequential, Model
from keras.layers.core import Activation, Flatten
from keras.layers import convolutional

defaults = {
            "board": 10,
            "filters_per_layer": 128,
            "layers": 12,
            "filter_width_1": 5
}
# copy defaults, but override with anything in kwargs
params = defaults
network = Sequential()
# create first layer
network.add(convolutional.Convolution2D(
      input_shape=(6, 10, 10),
      nb_filter=128,
      nb_row=5,
      nb_col=5,
      init='uniform',
      activation='relu',
      border_mode='same'))

# create all other layers
for i in range(2, 13):
     # use filter_width_K if it is there, otherwise use 3
     filter_key = "filter_width_%d" % i
     filter_width = params.get(filter_key, 3)

     # use filters_per_layer_K if it is there, otherwise use default value
     filter_count_key = "filters_per_layer_%d" % i
     filter_nb = params.get(filter_count_key, 128)

     network.add(convolutional.Convolution2D(
         nb_filter=filter_nb,
         nb_row=filter_width,
         nb_col=filter_width,
         init='uniform',
         activation='relu',
         border_mode='same'))

# the last layer maps each <filters_per_layer> feature to a number
network.add(convolutional.Convolution2D(
    nb_filter=1,
    nb_row=1,
    nb_col=1,
    init='uniform',
    border_mode='same'))
# reshape output to be board x board
network.add(Flatten())
# softmax makes it into a probability distribution
network.add(Activation('softmax'))
  • keras 1.2.0
  • python 2.7

给出以下例外:

  

ValueError:检查模型目标时出错:期望activation_1到   有形状(无,60)但有阵形(10,100)

训练数据集是(10,6,10,10)阵列,10 x 6层,每层是10x10阵列(棋盘),为什么模型需要(无,60)? 如果chagne input_shape=(6, 10, 10)input_shape=(10, 10, 10),将获得:

  

ValueError:检查模型输入时出错:预期   convolution2d_input_1有形状(无,10,10,10),但得到了数组   形状(10,6,10,10)

所有代码均为here

1 个答案:

答案 0 :(得分:0)

正如Matias在评论中所说,如果你添加

network.summary() 

您可能会注意到您的卷积应用于输入数据的前2个维度(即(6,10,10))。您的功能在第一维。默认情况下,当您使用tensorflow时,Keras将假定您的功能所在的维度是第三个,而不是第一个。因此,当你在(6,10,10)数组上应用128个过滤器时,输出将是(6,10,128),如果我理解的话,这不是你想要的。

由于这个原因,在最后一个卷积层的输出处,你得到一个(6,10,1)数组,它会像你期望的那样变平(,60)而不是(,100)。

有两种方法可以修复您的网络。您可以将输入数据更改为格式(10,10,6)。或者您使用data_format="channels_first"图层的Convolution2D()参数。

以下是第二个选项的代码:

from keras.models import Sequential, Model
from keras.layers.core import Activation, Flatten
from keras.layers import convolutional

defaults = {
            "board": 10,
            "filters_per_layer": 128,
            "layers": 12,
            "filter_width_1": 5
}
# copy defaults, but override with anything in kwargs
params = defaults
network = Sequential()
# create first layer
network.add(convolutional.Convolution2D(
      input_shape=(6, 10, 10),
      nb_filter=128,
      nb_row=5,
      nb_col=5,
      init='uniform',
      activation='relu',
      border_mode='same',
      data_format='channels_first'
      ))

# create all other layers
for i in range(2, 13):
     # use filter_width_K if it is there, otherwise use 3
     filter_key = "filter_width_%d" % i
     filter_width = params.get(filter_key, 3)

     # use filters_per_layer_K if it is there, otherwise use default value
     filter_count_key = "filters_per_layer_%d" % i
     filter_nb = params.get(filter_count_key, 128)

     network.add(convolutional.Convolution2D(
         nb_filter=filter_nb,
         nb_row=filter_width,
         nb_col=filter_width,
         init='uniform',
         activation='relu',
         border_mode='same',
         data_format='channels_first'))

# the last layer maps each <filters_per_layer> feature to a number
network.add(convolutional.Convolution2D(
    nb_filter=1,
    nb_row=1,
    nb_col=1,
    init='uniform',
    border_mode='same',
    data_format='channels_first'))
# reshape output to be board x board
network.add(Flatten())
# softmax makes it into a probability distribution
network.add(Activation('softmax'))
# display your network summary
network.summary()

修改

考虑到您的keras版本,您应该使用参数&#34; dim_ordering&#34;并将其设置为&#34; th&#34;。

我在keras documentation

中找到了这些信息
from keras.models import Sequential, Model
from keras.layers.core import Activation, Flatten
from keras.layers import convolutional

defaults = {
            "board": 10,
            "filters_per_layer": 128,
            "layers": 12,
            "filter_width_1": 5
}
# copy defaults, but override with anything in kwargs
params = defaults
network = Sequential()
# create first layer
network.add(convolutional.Convolution2D(
      input_shape=(6, 10, 10),
      nb_filter=128,
      nb_row=5,
      nb_col=5,
      init='uniform',
      activation='relu',
      border_mode='same',
      dim_ordering='th'
      ))

# create all other layers
for i in range(2, 13):
     # use filter_width_K if it is there, otherwise use 3
     filter_key = "filter_width_%d" % i
     filter_width = params.get(filter_key, 3)

     # use filters_per_layer_K if it is there, otherwise use default value
     filter_count_key = "filters_per_layer_%d" % i
     filter_nb = params.get(filter_count_key, 128)

     network.add(convolutional.Convolution2D(
         nb_filter=filter_nb,
         nb_row=filter_width,
         nb_col=filter_width,
         init='uniform',
         activation='relu',
         border_mode='same',
         dim_ordering='th'))

# the last layer maps each <filters_per_layer> feature to a number
network.add(convolutional.Convolution2D(
    nb_filter=1,
    nb_row=1,
    nb_col=1,
    init='uniform',
    border_mode='same',
    dim_ordering='th'))
# reshape output to be board x board
network.add(Flatten())
# softmax makes it into a probability distribution
network.add(Activation('softmax'))
# display your network summary
network.summary()