我正在尝试使用nengo中的Mnist数据集运行python存储库以进行数字分类,但由于此错误无法获得结果“AttributeError:'Conv2D'对象没有属性'subsample'我努力摆脱这个错误,但任何人都可以建议我解决此错误。
from __future__ import print_function
import os
os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32'
import nengo
import nengo_ocl
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import (
Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils
import nengo
from nengo_extras.keras import (
load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function
np.random.seed(1)
filename = 'mnist_spiking_cnn'
# --- Load data
img_rows, img_cols = 28, 28
nb_classes = 10
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')/128 - 1
X_test = X_test.astype('float32')/128 - 1
# --- Train model
if not os.path.exists(filename + '.h5'):
batch_size = 128
nb_epoch = 6
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
kmodel = Sequential()
softlif_params = dict(
sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(1, img_rows, img_cols)))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))
kmodel.add(Flatten())
kmodel.add(Dense(128))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))
kmodel.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
kmodel.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = kmodel.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
save_model_pair(kmodel, filename, overwrite=True)
else:
kmodel = load_model_pair(filename)
# --- Run model in Nengo
presentation_time = 0.2
model = nengo.Network()
with model:
u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
knet = SequentialNetwork(kmodel, synapse=nengo.synapses.Alpha(0.005))
nengo.Connection(u, knet.input, synapse=None)
input_p = nengo.Probe(u)
output_p = nengo.Probe(knet.output)
# --- image display
image_shape = kmodel.input_shape[1:]
display_f = image_display_function(image_shape)
display_node = nengo.Node(display_f, size_in=u.size_out)
nengo.Connection(u, display_node, synapse=None)
# --- output spa display
vocab_names = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR',
'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']
vocab_vectors = np.eye(len(vocab_names))
vocab = nengo.spa.Vocabulary(len(vocab_names))
for name, vector in zip(vocab_names, vocab_vectors):
vocab.add(name, vector)
config = nengo.Config(nengo.Ensemble)
config[nengo.Ensemble].neuron_type = nengo.Direct()
with config:
output = nengo.spa.State(len(vocab_names), subdimensions=10, vocab=vocab)
nengo.Connection(knet.output, output.input)
错误跟踪低于
AttributeError Traceback (most recent call last)
<ipython-input-14-6831ac9971de> in <module>()
5 with model:
6 u = nengo.Node(nengo.processes.PresentInput(X_train, presentation_time))
----> 7 knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
8 nengo.Connection(u, knet.input, synapse=None)
9
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in __init__(self, model, synapse, lif_type, **kwargs)
79 self.add_data_layer(np.prod(model.input_shape[1:]))
80 for layer in model.layers:
---> 81 self._add_layer(layer)
82
83 def _add_layer(self, layer):
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
99 for cls in type(layer).__mro__:
100 if cls in layer_adder:
--> 101 return layer_adder[cls](layer)
102
103 raise NotImplementedError("Cannot build layer type %r" %
~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
112 filters, biases = layer.get_weights()
113 filters = filters[..., ::-1, ::-1] # flip
--> 114 strides = layer.subsample
115
116 nf, nc, ni, nj = filters.shape
AttributeError: 'Conv2D' object has no attribute 'subsample'
答案 0 :(得分:0)
查看此https://forums.fast.ai/t/what-is-subsample-of-convolution2d-doing/3555
这表明旧版本的keras中的“子样本”已被新版本中的“ strides”所取代