[英]How to call functions of the class Sequential() if those are not in the source code?
I am quite new to machine learning and I am trying to implement my custom layer in keras. 我是机器学习的新手,我正在尝试在keras中实现我的自定义层。 I found a couple of tutorials and it seems comparatively straight forward.
我找到了几个教程,看起来比较直接。 What I do not understand, though, is how to implement my new custom layer in Sequential().
但我不明白的是如何在Sequential()中实现我的新自定义图层。 See for example this classification problem that I took from the tensorflow website( https://www.tensorflow.org/tutorials/keras/basic_text_classification ), posted here for your convenience:
例如,我从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)
Do I have to change the source code for keras.Sequential() or is there an easy way? 我是否必须更改keras.Sequential()的源代码或者有一个简单的方法吗?
Furthermore, looking at the source code for the class Sequential() made me wonder: I can't figure out how functions like 'summary()','compile()', 'fit()' and 'evaluate()' can be called if those are not even provided in the source code in this class. 此外,查看Sequential()类的源代码让我想知道:我无法弄清楚'summary()','compile()','fit()'和'evaluate()'这样的函数是怎样的如果在这个类的源代码中甚至没有提供它们,则调用它们。 Here is the source code for Sequential():
以下是Sequential()的源代码:
https://github.com/keras-team/keras/blob/a1397169ddf8595736c01fcea084c8e34e1a3884/keras/engine/sequential.py https://github.com/keras-team/keras/blob/a1397169ddf8595736c01fcea084c8e34e1a3884/keras/engine/sequential.py
Sequential
is a Model, and not a layer. Sequential
是模型,而不是图层。
The functions you mentioned ( summary
, compile
, fit
, evaluate
) are implemented in the Model class linked here , as Sequential is a subclass of Model. 您提到的函数(
summary
, compile
, fit
, evaluate
)在此处链接的Model类中实现,因为Sequential是Model的子类。
If you're writing a custom layer, you should be subclassing Layer instead, and not Model or Sequential. 如果您正在编写自定义图层,则应该改为继承Layer,而不是Model或Sequential。
You would need to implement build
, call
, and compute_output_shape
to create your own layer. 您需要实现
build
, call
和compute_output_shape
来创建自己的图层。
There's a few examples on the Keras documentation : Keras文档中有几个例子 :
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)
To use it, import the MyLayer class from whichever file you put it in, and then add it like the default Keras layers: 要使用它,请从您放入的文件中导入MyLayer类,然后像默认的Keras图层一样添加它:
from custom.layers import MyLayer
model = keras.Sequential()
model.add(MyLayer())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.