Embedding词向量

Go straight to code!

from tensorflow.keras.layers import Dense,Flatten, Embedding,Input
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import sequence,text
from tensorflow.keras.metrics import binary_accuracy
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import one_hot
import numpy as np

# define class labels
labels = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])

max_length=4
size=50
encode_docs=[one_hot(d,size) for d in docs]

Embedding词向量

paded_docs=pad_sequences(encode_docs,maxlen=max_length,padding='post')

Embedding词向量

input=Input(shape=(4,))
x=Embedding(size,8,input_length=max_length)(input)

x=Flatten()(x)
x=Dense(1,activation='sigmoid')(x)
model=Model(inputs=input,outputs=x)
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['acc'])
model.summary()

Embedding词向量

model.fit(paded_docs,labels,epochs=100,verbose=0)
loss,accuracy=model.evaluate(paded_docs,labels,verbose=0)

Embedding词向量

预测
test=one_hot('hei',50)
padded_test=pad_sequences([test],maxlen=max_length,padding='post')
padded_test
#array([[23,  0,  0,  0]])
model.predict(padded_test)
#array([[0.53474814]], dtype=float32)
上一篇:Substrate - 2 开始 - 2.2 架构(Architecture)


下一篇:python如何查看官方文档