Keras Embedding 实例

由于未提供博客具体内容,无法生成包含关键信息的摘要。
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:
 [[0 1 0 1 1 0 0]
 [1 1 1 1 1 1 1]] (2, 7)
2019-03-18 15: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.00076254  0.04154864]
  [-0.03167279 -0.00586861]
  [ 0.00076254  0.04154864]
  [-0.03167279 -0.00586861]
  [-0.03167279 -0.00586861]
  [ 0.00076254  0.04154864]
  [ 0.00076254  0.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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值