Machine Translation with Keras
Thushan Ganegedara
Data Scientist and Author

Definindo e usando uma camada Dense
dense = Dense(3, activation='softmax')
inp = Input(shape=(3,))
pred = dense(inp)
model = Model(inputs=inp, outputs=pred)
Definindo uma camada Dense com inicialização personalizada
from tensorflow.keras.initializers import RandomNormal
init = RandomNormal(mean=0.0, stddev=0.05, seed=6000)
dense = Dense(3, activation='softmax',
kernel_initializer=init, bias_initializer=init)
(batch size, input size)x = [[1, 6, 8], [8, 9, 10]] # um array 2x3(batch size, num classes)y = [[0.1, 0.3, 0.4, 0.2], [0.2, 0.5, 0.1, 0.2]] # um array 2x4np.argmax(y, axis=-1)np.argmax(y,axis=-1) produz [2,1]Dense processem entradas de séries temporaisdense_time = TimeDistributed(Dense(3, activation='softmax'))
inp = Input(shape=(2, 3))
pred = dense_time(inp)
model = Model(inputs=inp, outputs=pred)
(batch size, sequence length, input size)x = [[[1, 6], [8, 2], [1, 2]],
[[8, 9], [10, 8], [1, 0]]] # um array 2x3x2
(batch size, sequence length, num classes)y = [[[0.1, 0.5, 0.4], [0.8, 0.1, 0.1], [0.6, 0.2, 0.2]],
[[0.2, 0.5, 0.3], [0.2, 0.5, 0.3], [0.2, 0.8, 0.0]]] # um array 2x3x3
np.argmax(y, axis=-1)y = [[[0.1, 0.5, 0.4], [0.8, 0.1, 0.1], [0.6, 0.2, 0.2]],
[[0.2, 0.5, 0.3], [0.2, 0.5, 0.3], [0.2, 0.8, 0.0]]] # um array 2x3x3
classes = np.argmax(y, axis=-1) # um array 2 x 3
Iterando por dados distribuídos no tempo
for t in range(3):
# Obtenha o fatiamento na dimensão temporal t de y e classes
for prob, c in zip(y[:,t,:], classes[:,t]):
print("Prob: ", prob, ", Class: ", c)
Prob: [0.1 0.5 0.4] , Class: 1
Prob: [0.2 0.5 0.3] , Class: 1
Prob: [0.8 0.1 0.1] , Class: 0
...
Machine Translation with Keras