import numpy as np
from keras.models import Sequential
from keras.layers import Embedding, Masking
# Embedding和Masking都可以用来处理变长文本,Embedding只能过滤0
model = Sequential()
model.add(Embedding(input_dim=2,
output_dim=2,#将输入拆分成一个几维的量
input_length=7,))print('Embedding input shape:\n', model.layers[0].input_shape)print('Embedding output shape:\n', model.layers[0].output_shape)
model.compile('rmsprop','mse')
a=np.array([[0,1,0,1,1,0,0],[1,1,1,1,1,1,1]])print('input shape a:\n', a, a.shape)
result = model.predict(a)print('Embedded a:\n', result)print('shape Embedded a:\n', result.shape)
Using TensorFlow backend.
Embedding input shape:(None,7)
Embedding output shape:(None,7,2)input shape a:[[0101100][1111111]](2,7)2019-03-1815:23:16.590430: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Embedded a:[[[0.000762540.04154864][-0.03167279-0.00586861][0.000762540.04154864][-0.03167279-0.00586861][-0.03167279-0.00586861][0.000762540.04154864][0.000762540.04154864]][[-0.03167279-0.00586861][-0.03167279-0.00586861][-0.03167279-0.00586861][-0.03167279-0.00586861][-0.03167279-0.00586861][-0.03167279-0.00586861][-0.03167279-0.00586861]]]
shape Embedded a:(2,7,2)