tf.compat.v2.expand_dims

tf.compat.v2.expand_dims()
可以对tensor进行维度扩展。

1
2
3
4
5
tf.expand_dims(
input,
axis,
name=None
)

我应该在第一个维度进行扩展。
原来的维度 (2,588). —->(1,2,588)

tf 框架需要的数据类型是float32

调试以后。搭建的模型在测试的时候,可以正常的通过。之前主要是输入的shape不一样。

现在没有办法训练。经过调试以后程序在训练时一直处于decoder。肯定是这的问题。重新理解一下Decoder函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Decoder(keras.Model):#使用子类API实现
def __init__(self, vocab_size, embedding_units, decoding_units, batch_size):
super(Decoder, self).__init__()
self.batch_size = batch_size
self.decoding_units = decoding_units
self.embedding = keras.layers.Embedding(vocab_size, embedding_units) # 每一个单词转换成固定的向量,输出是三维的。
self.gru = keras.layers.GRU(self.decoding_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform')
self.fc = keras.layers.Dense(vocab_size)
self.attention = BahdanauAttention(self.decoding_units)



def call(self, x, hidden, encoding_outputs): #x是decoder中当前步的输入,hidden decoder中当前步的上一步的输出,encoding_outputs encoder中当前步的输出
# call 函数可以理解为对构造函数进行初始化。
# 先计算hidden encoding_outputs 的attention

# contex_vector.shape=(batch_size, units)

context_vector, attention_weights = self.attention(hidden, encoding_outputs)
print("正在计算Decoder-------------------------------------------------")
print(tf.shape(x))
x = self.embedding(x) # x 需要先进入embedding层, 目的是为了编码。会增加维度。在电信号上是不需要的。我使用的是expand_dim()进行维度的扩展。

# 把context_vector和x 相加,这样就会携带每一步的输入,
combined_x = tf.concat([tf.expand_dims(context_vector,1),x], axis=-1)

# 这一步就是decoder的返回值,和encode类似,就是outout和hidden
# output.shape = [batcha_size, 1 , decode_units]
# state.shape = [batch_size, decode_units ]
output, state = self.gru(combined_x)

# 这里对output进行reshape
# reshape后 output.shape=[batch_size, decoder_units]
output = tf.reshape(output, (-1, output.shape[2]))

# 输入到全链接层,输出为[batch_size,vocab_sie]
output = self.fc(output)

return output, state, attention_weights


decoder = Decoder(output_vocab_size, embedding_units, units, batch_size)

outputs = decoder(tf.random.uniform((batch_size,1)), sample_hidden, sample_output)

decoder_output, decoder_hidden, decoder_aw = outputs

print(decoder_output.shape, decoder_hidden.shape, decoder_aw.shape)