moyanzitto / keras-cn Goto Github PK
View Code? Open in Web Editor NEWChinese keras documents with more examples, explanations and tips.
Chinese keras documents with more examples, explanations and tips.
如题,因为我目前用的是earlystopping,虽然可以停止没必要的迭代,但我发现它只是探测一定次数的准确率未提升,所以如果我的准确率突然提升一段时间后一直逐渐下降,那它返回的最后一次的权重并不是最优时的权重。
以上是我目前想问的问题,望莫言大神帮忙解答一下,不甚感激!
在http://keras-cn.readthedocs.io/en/latest/models/sequential/中,def generate_arrays_from_file(path):的缩进出现错误
Hi 谢谢你们的贡献,准备推荐你们的这个文档给朋友的时候读了一下,发现Functional Model这个的翻译“泛型模型”有点不是很好理解。
Keras里面Functional Model的意思很好理解,可以利用函数嵌套函数的方式来构建模型,是指Functional Programming的style。而Functional Programming的中文翻译也有两种函数式编程和泛函编程,大概搜索了一下,前者较多,后者少一些。
对我来说,一看到泛函就想起泛函分析:https://zh.wikipedia.org/wiki/%E6%B3%9B%E5%87%BD ,很显然Functional Programming或Functional Model不是指泛函,不是指由一个向量空间到数值的映射。一个Functional Model更应该是一个张量到另一个张量的映射。
不知道你们是不是有其他理解,我的建议是使用“函数式模型”代替“泛型模型”,更容易理解,像keras本身一样更接地气,意义个人以为也会更准确。
imported by from keras.callbacks import Plotter
when I use callbacks in .fit()
model.fit(data, label, batch_size=32, nb_epoch=100, shuffle=True, validation_split=0.2, callbacks=[Plotter(show_plot_window=False, save_to_filepath="/tmp/last_plot.png")])
it's got ImportError
who know why is it?
I will be appreciate.
by dorbodwolf-lanzhou
比如在文本生成模型中,经常要将新生成的单词作为输入以生成下一个单词,keras能否实现类似的功能?怎么实现?
In the part named "如何在Keras中使用预训练的模型?"
from keras.applications.vgg16 impoprt VGG16
from keras.applications.vgg19 impoprt VGG19
from keras.applications.resnet50 impoprt ResNet50
from keras.applications.inception_v3 impoprt InceptionV3
"impoprt" should be "import".
我想写一个DNN的模型,用自编码器预训练
input_img = Input(shape=(39,))
encoded = Dense(32, activation='relu')(input_img)
encoded = Dense(16, activation='relu')(encoded)
encoded = Dense(8, activation='relu')(encoded)
decoded = Dense(16, activation='relu')(encoded)
decoded = Dense(32, activation='relu')(decoded)
decoded = Dense(39, activation='softmax')(decoded)
autoencoder = Model(input=input_img, output=decoded)
autoencoder.compile(optimizer='adadelta', loss='categorical_crossentropy')
这是预训练的模型,一共六层,保存在h5py中
h5py里的格式大概是这样的
HDF5 My.h5
Group '/'
Attributes:
'layer_names': 'input_1', 'dense_1', 'dense_2', 'dense_3', 'dense_4', 'dense_5', 'dense_6'
Group '/dense_1'
Attributes:
'weight_names': 'dense_1_W:0', 'dense_1_b:0'
Dataset 'dense_1_W:0'
Size: 32x39
MaxSize: 32x39
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'dense_1_b:0'
Size: 32
MaxSize: 32
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
现在我用DNN又写了一个模型
model=Sequential()
input_img = Input(shape=(39,))
model = Dropout(0.2)(input_img)
model = Dense(32, activation='relu')(model)
model = Dropout(0.2)(model)
model = Dense(16, activation='relu')(model)
model = Dropout(0.2)(model)
model = Dense(8, activation='relu')(model)
model = Dropout(0.2)(model)
model = Dense(4, activation='softmax')(model)
DNN = Model(input=input_img, output=model)
sgd = SGD(lr=0.01, momentum=0.8, decay=0.0, nesterov=False);
DNN.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy'])
weights_path='.../My.h5'
DNN.load_weights('.../My.h5',True)
提示我不能将自编码器的8节点到16节点放入DNN的8节点到4节点,ValueError: Shapes (8, 4) and (8, 16) are not compatible
keras更新到1.1.0不是可以将权重载入到不同模型中吗,我该怎么设置才能载入进去?
请问keras可以实现多标签分类么,即样本的标签为[1,0,1,1,...]这样,已经知道一共有50个类别,每个样本可能同时有多个标签,请问这种分类该如何实现呢?
when using “activity_regularizer” in keras with theno backend, some error report:
Traceback (most recent call last):
File "D:/working/AI/keras/snn/deepS/sys/deeps_mlp.py", line 238, in
validation_data=(x_test, y_test))
File "C:\Anaconda2\lib\site-packages\keras\models.py", line 429, in fit
sample_weight=sample_weight)
File "C:\Anaconda2\lib\site-packages\keras\engine\training.py", line 1081, in fit
self._make_train_function()
File "C:\Anaconda2\lib\site-packages\keras\engine\training.py", line 698, in _make_train_function
training_updates = self.optimizer.get_updates(trainable_weights, self.constraints, self.total_loss)
File "C:\Anaconda2\lib\site-packages\keras\optimizers.py", line 321, in get_updates
grads = self.get_gradients(loss, params)
File "C:\Anaconda2\lib\site-packages\keras\optimizers.py", line 53, in get_gradients
grads = K.gradients(loss, params)
File "C:\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 576, in gradients
return T.grad(loss, variables)
File "C:\Anaconda2\lib\site-packages\theano\gradient.py", line 436, in grad
raise TypeError("cost must be a scalar.")
TypeError: cost must be a scalar.
The model is very simple:
keras.regularizers.WeightRegularizer(l1=0.01, l2=0.01)
keras.regularizers.ActivityRegularizer(l1=0.01, l2=0.01)
model = Sequential()
model.add(Dense(16, input_shape=(inputsize,),W_regularizer=l1l2(0.01,0.01),b_regularizer=l1l2(0.01,0.01)))
model.add(Activation('relu'))
model.add(Dropout(0.8))
model.add(Dense(1,W_regularizer=l1l2(0.01,0.01),b_regularizer=l1l2(0.01,0.01),activity_regularizer=activity_l1l2(0.01,0.01)))
model.add(Activation('sigmoid'))
model.summary()
so, how can i use the "activity_regularizer" ?
代码部分:
language_model = Sequential()
language_model.add(Embedding(vocab_size+2, EMBEDDING_DIM, mask_zero=True, weights=[embedding_matrix] ,name='embedding_layer'))
language_model.add(Masking(mask_value=PADDING,name='masking_layer'))
language_model.add(LSTM(output_dim=OTHERS, return_sequences=True,name='gru_layer'))
language_model.add(TimeDistributedDense(OUTPUT_TYPES_NUM,activation='softmax',name='softmax_layer'))
测试用例:
x_test=np.asarray([[0,0,0,2,3,4,5,6],[0,0,0,0,9,8,3,4],[0,0,0,0,0,9,8,3]])
Softmax 的输出结果:
Dense Results:
(3L, 8L, 10L)
[[[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10468332 0.09065267 0.10465907 0.10914598 0.09866636 0.10089909
0.09287892 0.11226305 0.09915846 0.08699314]
[ 0.10090157 0.1001429 0.10266486 0.10592359 0.08957883 0.1033702
0.10338669 0.09631021 0.09575413 0.10196707]
[ 0.1009273 0.10819422 0.09471504 0.09420478 0.10287543 0.09454429
0.08861585 0.10622577 0.10038092 0.10931643]
[ 0.0961623 0.10527957 0.09671341 0.09006541 0.10609487 0.09137738
0.08720418 0.1061098 0.1045992 0.11639385]
[ 0.09365714 0.09961554 0.10184344 0.09357952 0.10447578 0.09096033
0.08860005 0.11356424 0.10304262 0.11066134]]
[[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.08880188 0.11206884 0.08541028 0.09300748 0.11226043 0.08611012
0.102557 0.11383081 0.10294707 0.10300611]
[ 0.10394291 0.09647608 0.10178506 0.10291336 0.10539917 0.0867321
0.09617651 0.10704685 0.09978233 0.09974565]
[ 0.0968347 0.10330293 0.10114764 0.09074623 0.09358761 0.09601314
0.10900775 0.10108642 0.10264133 0.10563225]
[ 0.09343617 0.10481153 0.09716355 0.09031702 0.10999709 0.09630899
0.09260204 0.10974768 0.1030058 0.10261014]]
[[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.10432124 0.09956408 0.09805973 0.10185226 0.10514925 0.09005494
0.09369726 0.10397689 0.10004393 0.10328046]
[ 0.08880188 0.11206884 0.08541028 0.09300748 0.11226043 0.08611012
0.102557 0.11383081 0.10294707 0.10300611]
[ 0.10394291 0.09647608 0.10178506 0.10291336 0.10539917 0.0867321
0.09617651 0.10704685 0.09978233 0.09974565]
[ 0.0968347 0.10330293 0.10114764 0.09074623 0.09358761 0.09601314
0.10900775 0.10108642 0.10264133 0.10563225]]]
最终结果:
[[4 4 4 7 3 9 9 7]
[4 4 4 4 7 7 6 4]
[4 4 4 4 4 7 7 6]]
此外,有个比较奇怪的发现,输入必须的Padding必须加在pre位置上,求大大门指导
各位亲爱的keras用户以及keras-cn用户,大家好!
为了丰富大家的交流,我们现在准备把github issues利用起来~各位可以在这里:
当然,尽量聊技术~
发布帖子打好标签,方便大家寻找,格式为
【标签】标题
建议:
Traceback (most recent call last):
File "F:/python/ObGraspDec/GraspRegression/GraspRegression.py", line 102, in
model.fit(X, Y, batch_size=1, nb_epoch=1, verbose=1, shuffle=False)
File "D:\anaconda2\lib\site-packages\keras\engine\training.py", line 1106, in fit
callback_metrics=callback_metrics)
File "D:\anaconda2\lib\site-packages\keras\engine\training.py", line 824, in _fit_loop
outs = f(ins_batch)
File "D:\anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 717, in call
return self.function(*inputs)
File "D:\anaconda2\lib\site-packages\theano-0.9.0.dev3-py2.7.egg\theano\compile\function_module.py", line 879, in call
storage_map=getattr(self.fn, 'storage_map', None))
File "D:\anaconda2\lib\site-packages\theano-0.9.0.dev3-py2.7.egg\theano\gof\link.py", line 325, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "D:\anaconda2\lib\site-packages\theano-0.9.0.dev3-py2.7.egg\theano\compile\function_module.py", line 866, in call
self.fn() if output_subset is None else
File "D:\anaconda2\lib\site-packages\theano-0.9.0.dev3-py2.7.egg\theano\gof\op.py", line 866, in rval
r = p(n, [x[0] for x in i], o)
File "D:\anaconda2\lib\site-packages\theano-0.9.0.dev3-py2.7.egg\theano\compile\ops.py", line 708, in perform
(axis, x.shape[axis]))
ValueError: Dimension 3 in Rebroadcast's input was supposed to be 1 (got 55 instead)
Apply node that caused the error: Rebroadcast{?,?,?,1}(GpuContiguous.0)
Toposort index: 135
Inputs types: [CudaNdarrayType(float32, (True, True, True, False))]
Inputs shapes: [(1, 1, 1, 55)]
Inputs strides: [(0, 0, 0, 1)]
Inputs values: ['not shown']
Inputs type_num: ['']
Outputs clients: [[GpuElemwise{true_div,no_inplace}(Rebroadcast{?,?,?,1}.0, GpuElemwise{Composite{sqrt((i0 + i1))},no_inplace}.0), GpuElemwise{true_di
model=AlexNet(weights_path='../weights/regression.h5')
Indice=range(0,885)
DataIndiceList = []
for i in range(0,885,10):
DataIndiceList.append(Indice[i:i + 10])
for e in range(50):
print("epoch %d" % e)
for IndiceList in DataIndiceList:
X, Y = DataUtils.LoadAugData(IndiceList[0], IndiceList[-1])
ShuffleList=range(0,len(Y))
random.shuffle(ShuffleList)
X = X[ShuffleList]
Y = Y[ShuffleList]/227.0
model.fit(X, Y, batch_size=1, nb_epoch=1, verbose=1, shuffle=False)
`get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],
[model.layers[3].output])
layer_output = get_3rd_layer_output([X, 0])[0]
layer_output = get_3rd_layer_output([X, 1])[0]`
I've found that the result of two modes are different.
what is the different between test mode and train mode?
what does these two modes mean?
thank u
我将keras example中的例子imdb_cnn.py运行三次,得到三次不同的结果,输出test accuracy得到的结果分别为:
0.87996,
0.87632,
0.87644
请问怎样做才能使最终输出的结果完全稳定下来,每次运行输出结果一模一样,完全重现?
您好
想請問我將模型訓練好之後,怎麼利用自己的圖片去辨識分類呢?
要下的指令是什麼?
不好意思我不是本科生,所以問題可能有點笨,謝謝。
如题所述,求解答
merged = Merge([model_left, model_right,model3], mode='concat') #merge
modelm = Sequential()
modelm.add(merged) # add merge
modelm.add(Dense(128, activation='tanh'))
modelm.add(Dense(num_label, activation='softmax'))
get_feature=theano.function([modelm.layers[0].input],modelm.layers[1].output(train=True),allow_input_downcast=False)
feature = get_feature(x)
Not really an issue, but I haven't found any documentation on how to access the hidden state in LSTM RNN.
Context: I want to capture the semantic meaning of a sentence, by feeding in the Glove word embedding vectors ( https://github.com/fchollet/keras/blob/master/examples/pretrained_word_embeddings.py ) into the RNN and when the sentence ends, the final hidden state captures the so-called sentence embedding. I want to access this vector obtained from the final hidden state.
大家好,请问一下model.fit()函数将verbose设为1时输出的准确率是当前输入的batch的准确率还是整个训练集的准确率呢,我的batchsize时32,准确率输出是0.0347,这个应该不是batch的准确率吧
下面是我的代码:
import os
os.environ['THEANO_FLAGS']='device=gpu0'
import keras
from keras.layers import Input, Convolution2D, BatchNormalization, Activation, GlobalAveragePooling2D, Dense, merge
from keras.models import Model
from keras.utils.visualize_util import plot
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
batch_size = 128
nb_classes = 10
nb_epoch = 200
data_augmentation = False
learning_rate = 0.001
# input image dimensions
img_rows, img_cols = 32, 32
# the CIFAR10 images are RGB
img_channels = 3
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# 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)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# X_train /= 255.0
# X_test /= 255.0
X_train = (X_train - np.mean(X_train))/np.std(X_train)
X_test = (X_test - np.mean(X_test))/np.std(X_test)
n=3
def original_resnet_block(nb_filter, subsample):
def f(input):
conv1 = Convolution2D(nb_filter, 3, 3, border_mode='same', subsample=subsample)(input)
bn2 = BatchNormalization(momentum=0.9, axis=1)(conv1)
relu3 = Activation('relu')(bn2)
conv4 = Convolution2D(nb_filter, 3, 3, border_mode='same', subsample=(1, 1))(relu3)
bn5 = BatchNormalization(momentum=0.9, axis=1)(conv4)
if subsample == (1,1):
shortcut = input
elif subsample == (2,2):
shortcut = Convolution2D(nb_filter, 3, 3, border_mode='same', subsample=subsample)(input)
else:
print ('Error! Exit now')
exit(-1)
merge6 = merge([shortcut, bn5], mode='sum')
return merge6
return f
ipt = Input(shape=(3, 32, 32))
bn = BatchNormalization(momentum=0.9, axis=1)(ipt)
conv = Convolution2D(16, 3, 3, border_mode='same', subsample=(1, 1))(bn)
bn2 = BatchNormalization(momentum=0.9, axis=1)(conv)
relu = Activation('relu')(bn2)
# block 1
block1 = relu
for i in range(n):
if i == n-1:
subsample = (2,2)
nb_filter = 32
else:
subsample = (1,1)
nb_filter = 16
block1 = original_resnet_block(nb_filter,subsample=subsample)(block1)
# block 2
block2 = block1
for i in range(n):
if i == n-1:
subsample = (2,2)
nb_filter = 64
else:
subsample = (1,1)
nb_filter = 32
block2 = original_resnet_block(nb_filter,subsample=subsample)(block2)
# block 3
block3 = block2
for i in range(n):
subsample = (1,1)
nb_filter = 64
block3 = original_resnet_block(nb_filter,subsample=subsample)(block3)
global_average_pool = GlobalAveragePooling2D()(block3)
dense = Dense(10)(global_average_pool)
softmax = Activation('softmax')(dense)
model = Model(input=ipt, output=softmax)
plot(model, to_file='model.png')
model.summary()
optimizer = SGD(lr = learning_rate, momentum = 0.9, decay = 0.0, nesterov = True)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
if not data_augmentation:
print('Not using data augmentation.')
model.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test),
shuffle=True)
else:
print('Using real-time data augmentation.')
# this will do preprocessing and realtime data augmentation
datagen_train = ImageDataGenerator(
rescale=1./255,
featurewise_center=True, # set input mean to 0 over the dataset
shear_range=0.2,
zoom_range=0.2) # randomly flip images
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen_train.fit(X_train)
datagen_test = ImageDataGenerator(
rescale=1./255,
featurewise_center=True # set input mean to 0 over the dataset
)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen_test.fit(X_test)
# fit the model on the batches generated by datagen.flow()
model.fit_generator(datagen_train.flow(X_train, Y_train,
batch_size=batch_size),
samples_per_epoch=X_train.shape[0],
nb_epoch=nb_epoch,
validation_data=datagen_test.flow(X_test, Y_test,
batch_size=batch_size),
nb_val_samples=X_test.shape[0] )
Hi,
when I am training keras in windows 64bit, I got the error as below:
AssertionError: AbstractConv2d Theano optimizationfailed: there is no implementation available supporting the requested options.Did you exclude both "conv_dnn" and "conv_gemm" from theoptimizer? If on GPU, is cuDNN available and does the GPU support it? If onCPU, do you have a BLAS library installed Theano can link against?
then I fixed it by config pre-compiled openBLAS( https://sourceforge.net/projects/openblas/files/v0.2.8/
), the context of my config file .theanorc.txt is below:
[global]
floatX = float32
device = cpu
optimizer = None
THEANO_FLAGS='optimizer_excluding=conv_dnn, optimizer_excluding=conv_gemm'
[blas]
ldflags = -LC:\OpenBLAS\lib –lopenblas
but when I train cnn again, it got ImportError
ImportError: ('The following error happened while compiling the node', Elemwise{round_half_away_from_zero,no_inplace}(Softmax.0), '\n', 'DLL load failed: \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xb3\xcc\xd0\xf2\xa1\xa3', '[Elemwise{round_half_away_from_zero,no_inplace}()]')
I cannot find trick of this trouble, who can provide help? I am appreciating your kindness.
from dorbodwolf-兰州
对不定长文本分类,我想请教一下当padding成统一长度maxlen时,这个maxlen是如何取值的?目前我试验样本长短差距较大,短的几十个字,长的上千的字,请假一下maxlen取值有何经验?谢谢!
keras.layers.wrappers.TimeDistributed(layer)
该包装器可以把一个层应用到输入的每一个时间步上
参数
layer:Keras层对象
输入至少为3D张量,下标为1的维度将被认为是时间维
例如,考虑一个含有32个样本的batch,每个样本都是10个向量组成的序列,每个向量长为16,则其输入维度为(32,10,16),其不包含batch大小的input_shape为(10,16)
我们可以使用包装器TimeDistributed包装Dense,以产生针对各个时间步信号的独立全连接:
model = Sequential()
model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
model.add(TimeDistributed(Dense(32)))
具体问题是:
“把一个层应用到输入的每一个时间步上”改怎么理解? 比如dense层,本来就是应用的全部输入上,那么和应用到输入的每个时间步上是什么区别? “针对各个时间步信号的独立全连接”这里的“独立全连接”是什么意思?
例子中
model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
这个每次输入是长度16的1个向量,输出是长度8的1个向量?这个是否正确?
谢谢!
模型,函数式模型的predict_generator的方法定义写成了fit_generator了
应该改成predict_generator(self,generator, steps, max_q_size=10, workers=1, pickle_safe=False, verbose=0)
https://github.com/MoyanZitto/keras-cn/blob/master/docs/getting_started/concepts.md
在'th'与'tf' 这一节当中提到,“Keras默认的后端是Theano”,可是其他文档显示Keras默认的后端是 Tensorflow。请问这个应该怎么解释?
如标题,望大神能帮忙解答下,因为我在跑实验时候发现上一次迭代的验证准确率80%,下一次迭代突然降到60%。
大家好,请问如何利用一个已有的网络,比如VGG,训练一张图片,使得它在狗这个类别上的概率最大呢
在“http://keras-cn.readthedocs.io/en/latest/getting_started/keras_linux/”页中,
theanorc文件的配置开头少了[global]:
[global]
openmp=False
device = gpu
floatX = float32
allow_input_downcast=True
[lib]
cnmem = 0.8
[blas]
ldflags= -lopenblas
[nvcc]
fastmath = True
在Docs » 其他重要模块 » 预训练模型 » ApplicationInceptionV3模型
中,模型的默认输入尺寸时229x229
有误,原文是299x299.
具体的代码是这个
model2.add(TimeDistributed(Convolution2D(64, 3, 3,
border_mode='valid',
W_regularizer=l2(0.01),
activity_regularizer=activity_l2(0.01))))
如果去掉activity_regularizer就正常了,tf和th都试了,都会报这个异常
大家好,我在训练CNN时候出现如下异常:
Traceback (most recent call last):
File "trainCNN.py", line 91, in
model.add(Flatten())
File "C:\Anaconda2\lib\site-packages\keras\models.py", line 308, in add
output_tensor = layer(self.outputs[0])
File "C:\Anaconda2\lib\site-packages\keras\engine\topology.py", line 514, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "C:\Anaconda2\lib\site-packages\keras\engine\topology.py", line 572, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "C:\Anaconda2\lib\site-packages\keras\engine\topology.py", line 152, in create_node
output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
File "C:\Anaconda2\lib\site-packages\keras\layers\core.py", line 402, in get_output_shape_for
'(got ' + str(input_shape[1:]) + '. '
Exception: The shape of the input to "Flatten" is not fully defined (got (16, 0, 0). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
构建模型的代码如下:
`
"""
BUILD CNN NETWORK
"""
model = Sequential()
model.add(Convolution2D(4, 3, 3, border_mode='valid',batch_input_shape=(100, 5, 16, 16), dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))
model.add(Convolution2D(8,3,3, border_mode='valid', dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))
model.add(Convolution2D(16, 3, 3, border_mode = 'valid', dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))
"""
train the model
"""
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(data, label, batch_size=100, nb_epoch=10, shuffle=True, verbose=1, validation_split=0.2)
`
背景:我将大约50W张图片做一个vgg的finetune。
使用,fit_generator,这个图片读取速度感觉太慢了。
硬件是K80的GPU,nvidia-smi看GPU的使用率很低,接近0.
是不是很多时间浪费在了数据的IO上面?
caffe之类的框架有lmdb等快速索引的工具。
keras文章中支持h5文件。能不能给出一个使用h5的demo ~~~
在使用Keras中deep-learning-models里的ResNet50模型时,出现了这样的错误
Using TensorFlow backend.
Loading data...
Traceback (most recent call last):
x_train shape: (830, 256, 512)
File "/home/liutingxi/Research/VED/Experiment/soccer_resnet.py", line 35, in
Build model...
model = ResNet50(weights='imagenet',input_shape=(256,512))
File "/home/liutingxi/Research/VED/Experiment/resnet50.py", line 192, in ResNet50
data_format=K.image_data_format(),
AttributeError: 'module' object has no attribute 'image_data_format'
后端用的是TensorFlow 0.9.0,但是
#fchollet/deep-learning-models#45
中用的1.0.0版本出现了同样的问题,换成Theano也是,各位大神有没有什么看法呢?感激不尽
在class SimpleRNN(Recurrent):
...里面有一个
if self.stateful:
self.reset_states()
else:
# initial states: all-zero tensor of shape (output_dim)
self.states = [None]
而在class LSTM(Recurrent):
if self.stateful:
self.reset_states()
else:
# initial states: 2 all-zero tensors of shape (output_dim)
self.states = [None, None]
怎么理解这个self.state,在lstm类代码中states值有四个?
def step(self, x, states):
h_tm1 = states[0]
c_tm1 = states[1]
B_U = states[2]
B_W = states[3]
model.save('MemNN_model_epoch-%d.hdf5' %(nb_epoch)) #save
model=load_model('MemNN_model_epoch-1.hdf5') #load
我的第一层是Embedding层,为何这样导入model会出错?
err:
You are trying to load a weight file containing 5 layers into a model with 6 layers
Traceback (most recent call last):
File "F:/python/ObGraspDec/GraspRegression.py", line 92, in
estimator.fit(X,Y)
File "D:\anaconda2\lib\site-packages\keras\wrappers\scikit_learn.py", line 135, in fit
**self.filter_sk_params(self.build_fn.call))
TypeError: call() takes at least 2 arguments (1 given)
model=AlexNet(weights_path=None, ImageSize=ImageSize, NumOuput=NumOuput)
model.compile(loss='mean_squared_error', optimizer='adam')
seed = 7
np.random.seed(seed)
estimator = KerasRegressor(build_fn=model, nb_epoch=nb_epoch, batch_size=batch_size)
estimator.fit(X,Y)
错误:
将vgg16最后一个卷积块前的卷积层参数冻结,原文为:
for layer in model.layers[:25]: layer.trainable = False
应该为:
for layer in model.layers[:15]: layer.trainable = False
在“http://keras-cn.readthedocs.io/en/latest/getting_started/keras_linux/”页中:
”之后source gedit /etc/bash.bashrc即可“,这句可能是笔误。
感谢翻译,不过今天在看到分词器Tokenizer的时候对照英文文档发现有错漏的地方。
最后一个类方法sequences_to_matrix(sequences)
的第一个参数写成了texts:待向量化的文本列表
,而原文是sequences: list of sequences to vectorize.
所以应该改为sequences: 待向量化的序列列表
才对。
另外,英文文档中类方法下方还有Tokenizer的四个属性(word_counts,word_docs,word_index,document_count)介绍中文文档中并没有。
中文文档是这样说的:
一个Keras的模型有两个模式:训练模式和测试模式。一些正则机制,如Dropout,L1/L2正则项在测试模式下将不被启用。
另外,训练误差是训练数据每个batch的误差的平均。在训练过程中,每个epoch起始时的batch的误差要大一些,而后面的batch的误差要小一些。另一方面,每个epoch结束时计算的测试误差是由模型在epoch结束时的状态决定的,这时候的网络将产生较小的误差。
【Tips】可以通过定义回调函数将每个epoch的训练误差和测试误差并作图,如果训练误差曲线和测试误差曲线之间有很大的空隙,说明你的模型可能有过拟合的问题。当然,这个问题与Keras无关。【@BigMoyan】
英文文档是这样的
A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time.
Besides, the training loss is the average of the losses over each batch of training data. Because your model is changing over time, the loss over the first batches of an epoch is generally higher than over the last batches. On the other hand, the testing loss for an epoch is computed using the model as it is at the end of the epoch, resulting in a lower loss.
这里的测试误差是指划分的validation data还是指testing data?
I am using Tian X to train AlexNet. The input is 3227227 and when I set the batch_size in fit function, it will break down. As far as I know, Titan X has 12 GB memory. When I use a GPU with 1.5 GB memory, the batch_size can be 64. Why can't I set the batch_size larger than 512?
Epoch 9/12
66/3300 [..............................] - ETA: 187s - loss: 1.3111 - acc: 0.6
132/3300 [>.............................] - ETA: 183s - loss: 1.3862 - acc: 0.6
198/3300 [>.............................] - ETA: 180s - loss: 1.3752 - acc: 0.6
264/3300 [=>............................] - ETA: 176s - loss: 1.3745 - acc: 0.6
330/3300 [==>...........................] - ETA: 172s - loss: 1.3791 - acc: 0.6
396/3300 [==>...........................] - ETA: 168s - loss: 1.3531 - acc: 0.6
462/3300 [===>..........................] - ETA: 164s - loss: 1.4262 - acc: 0.5
528/3300 [===>..........................] - ETA: 161s - loss: 1.4514 - acc: 0.5
594/3300 [====>.........................] - ETA: 157s - loss: 1.4463 - acc: 0.5
660/3300 [=====>........................] - ETA: 153s - loss: 1.4639 - acc: 0.5
726/3300 [=====>........................] - ETA: 149s - loss: 1.4817 - acc: 0.5
792/3300 [======>.......................] - ETA: 145s - loss: 1.5080 - acc: 0.5
858/3300 [======>.......................] - ETA: 141s - loss: 1.5133 - acc: 0.5
924/3300 [=======>......................] - ETA: 138s - loss: 1.4968 - acc: 0.5
990/3300 [========>.....................] - ETA: 134s - loss: 1.5090 - acc: 0.5
1056/3300 [========>.....................] - ETA: 130s - loss: 1.5078 - acc: 0.5
1122/3300 [=========>....................] - ETA: 126s - loss: 1.4920 - acc: 0.5
1188/3300 [=========>....................] - ETA: 122s - loss: 1.4766 - acc: 0.5
1254/3300 [==========>...................] - ETA: 118s - loss: 1.4613 - acc: 0.5
1320/3300 [===========>..................] - ETA: 115s - loss: 1.4620 - acc: 0.5
1386/3300 [===========>..................] - ETA: 111s - loss: 1.4687 - acc: 0.5
1452/3300 [============>.................] - ETA: 107s - loss: 1.4600 - acc: 0.5
1518/3300 [============>.................] - ETA: 103s - loss: 1.4676 - acc: 0.5
1584/3300 [=============>................] - ETA: 99s - loss: 1.4661 - acc: 0.57
1650/3300 [==============>...............] - ETA: 96s - loss: 1.4704 - acc: 0.57
1716/3300 [==============>...............] - ETA: 92s - loss: 1.4654 - acc: 0.57
1782/3300 [===============>..............] - ETA: 88s - loss: 1.4604 - acc: 0.57
1848/3300 [===============>..............] - ETA: 84s - loss: 1.4622 - acc: 0.56
1914/3300 [================>.............] - ETA: 80s - loss: 1.4664 - acc: 0.56
1980/3300 [=================>............] - ETA: 76s - loss: 1.4751 - acc: 0.56
2046/3300 [=================>............] - ETA: 72s - loss: 1.4672 - acc: 0.56
2112/3300 [==================>...........] - ETA: 69s - loss: 1.4711 - acc: 0.56
2178/3300 [==================>...........] - ETA: 65s - loss: 1.4716 - acc: 0.56
2244/3300 [===================>..........] - ETA: 61s - loss: 1.4764 - acc: 0.55
2310/3300 [====================>.........] - ETA: 57s - loss: 1.4698 - acc: 0.55
2376/3300 [====================>.........] - ETA: 53s - loss: 1.4797 - acc: 0.55
2442/3300 [=====================>........] - ETA: 49s - loss: 1.4791 - acc: 0.56
2508/3300 [=====================>........] - ETA: 46s - loss: 1.4839 - acc: 0.55
2574/3300 [======================>.......] - ETA: 42s - loss: 1.4884 - acc: 0.55
2640/3300 [=======================>......] - ETA: 38s - loss: 1.4848 - acc: 0.55
2706/3300 [=======================>......] - ETA: 34s - loss: 1.4841 - acc: 0.55
2772/3300 [========================>.....] - ETA: 30s - loss: 1.4817 - acc: 0.55
2838/3300 [========================>.....] - ETA: 26s - loss: 1.4770 - acc: 0.56
2904/3300 [=========================>....] - ETA: 23s - loss: 1.4796 - acc: 0.55
2970/3300 [==========================>...] - ETA: 19s - loss: 1.4786 - acc: 0.56
3036/3300 [==========================>...] - ETA: 15s - loss: 1.4703 - acc: 0.56
3102/3300 [===========================>..] - ETA: 11s - loss: 1.4700 - acc: 0.56
from keras.layers import Input, Dense, merge
from keras.models import Model
from keras import backend as K
a = Input(shape=(2,), name='a')
b = Input(shape=(2,), name='b')
a_rotated = Dense(2, activation='linear')(a)
def cosine(x):
axis = len(x[0]._keras_shape)-1
dot = lambda a, b: K.batch_dot(a, b, axes=axis)
return dot(x[0], x[1]) / K.sqrt(dot(x[0], x[0]) * dot(x[1], x[1]))
cosine_sim = merge([a_rotated, b], mode=cosine, output_shape=lambda x: x[:-1])
model = Model(input=[a, b], output=[cosine_sim])
model.compile(optimizer='sgd', loss='mse')
import numpy as np
a_data = np.asarray([[0, 1], [1, 0], [0, -1], [-1, 0]])
b_data = np.asarray([[1, 0], [0, -1], [-1, 0], [0, 1]])
targets = np.asarray([1, 1, 1, 1])
model.fit([a_data, b_data], [targets], nb_epoch=1000)
print(model.layers[2].W.get_value())
http://keras-cn.readthedocs.io/en/latest/other/callbacks/#modelcheckpoint
这里给出的例子中的反括号“}” 位置有误,应该如同最新英文版
weights.{epoch:02d}-{val_loss:.2f}.hdf5
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.