我对机器学习非常陌生,我正在尝试在keras中实现我的自定义层。我找到了一些教程,似乎比较简单。不过,我不了解的是如何在Sequential()中实现我的新自定义层。例如,请参阅我从tensorflow网站(https://www.tensorflow.org/tutorials/keras/basic_text_classification)处理的此分类问题,为方便起见,发布在此处:
from __future__ import absolute_import, division, print_function
import tensorflow as tf
from tensorflow import keras
import numpy as np
imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
# A dictionary mapping words to an integer index
word_index = imdb.get_word_index()
# The first indices are reserved
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2 # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?') for i in text])
train_data = keras.preprocessing.sequence.pad_sequences(train_data,
value=word_index["<PAD>"],
padding='post',
maxlen=256)
test_data = keras.preprocessing.sequence.pad_sequences(test_data,
value=word_index["<PAD>"],
padding='post',
maxlen=256)
# input shape is the vocabulary count used for the movie reviews (10,000 words)
vocab_size = 10000
model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.summary()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
x_val = train_data[:10000]
partial_x_train = train_data[10000:]
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]
history = model.fit(partial_x_train,
partial_y_train,
epochs=40,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1)
results = model.evaluate(test_data, test_labels)
print(results)
我是否必须更改keras.Sequential()的源代码,还是有一种简单的方法?
此外,查看类Sequential()的源代码使我感到困惑:我无法弄清楚'summary()','compile()','fit()'和'evaluate() ”)(如果此类的源代码中甚至没有提供)都可以调用。这是Sequential()的源代码:
答案 0 :(得分:0)
Sequential
是一个模型,而不是一个层。
您提到的功能(summary
,compile
,fit
,evaluate
)在Model class linked here中实现,因为Sequential是Model的子类。
如果要编写自定义图层,则应该改为继承Layer,而不是Model或Sequential。
您需要实现build
,call
和compute_output_shape
来创建自己的图层。
有一些examples on the Keras documentation:
from keras import backend as K
from keras.layers import Layer
class MyLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='uniform',
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this at the end
def call(self, x):
return K.dot(x, self.kernel)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
要使用它,请从您放入的文件中导入MyLayer类,然后像默认的Keras图层一样添加它:
from custom.layers import MyLayer
model = keras.Sequential()
model.add(MyLayer())