SquareFace Blog


  • 首页

  • 关于

  • 标签

  • 归档

  • 搜索

tf.compat.v2.expand_dims

发表于 2019-08-20
字数统计: 461 | 阅读时长 ≈ 2

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)

embedding记录

发表于 2019-08-19
字数统计: 91 | 阅读时长 ≈ 1

问题:在embedding层,会报错。报错内容

1
indices[0,0] = -13 is not in [0, 600) [Op:ResourceGather] name: encoder_3/embedding_2/embedding_lookup

意思就是 -13这个数值不在 这里数组里面

查看keras.layers.Embedding()的底层代码,发现这一层是专门用在自然语言处理的方向的。。。

需要把1维的数据—》3 维

这应该怎么弄呢???

evaluate(input_sentence)

发表于 2019-08-18
字数统计: 430 | 阅读时长 ≈ 1

embedding_units = 256 # 每个word转换成embedding是多少。
第一次掉用这个参数是在,构建Encoder模型中,keras.layers.Embedding(vocab_size, embedding_units)这个函数中使用。
keras.layers.Embedding(input_dim, output_dim,)
将正整数(索引值)转换为固定尺寸的稠密向量。 例如: [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]
该层只能用作模型中的第一层
input_dim: int > 0。词汇表大小, 即,最大整数 index + 1。
output_dim: int >= 0。词向量的维度。

-输入尺寸

尺寸为 (batch_size, sequence_length) 的 2D 张量。

-输出尺寸

尺寸为 (batch_size, sequence_length, output_dim) 的 3D 张量。

max_length_input 这个参数是输入的每个序列长度,即文档里面的sequence_length。

Encoder 模型有两层,第一层是embedding 是用来预处理的,有点像归一化。进入embedding后数据的shape会发生变化,如上所说的输出尺寸。

        # 第二层是GRU层。
使用encoder函数,会有两个输出,一个是output(就是上面出的输出尺寸) 一个是hidden。hidden.shape=(batch_size, units)

units=1024, 这个代表的是中间使用RNN的个数

这是在构造函数里面的GRU()初始化设置。

self.gru = keras.layers.GRU(self.encoding_units,return_sequences=True,return_state=True, recurrent_initializer=’glorot_uniform’)

encoding_units 是GRU的输出尺寸,recurrent_regularizer: 运用到 recurrent_kernel 权值矩阵的正则化函数

####call方法
call方法 可以理解为使用构造函数,在实例化这个方法的时候可以直接调用。
output, state = self.gru(x, initial_state=hidden)
您可以通过使用关键字参数 initial_state 调用它们来符号化地指定 RNN 层的初始状态。 initial_state 的值应该是表示 RNN 层初始状态的张量或张量列表。

检查了一下 input_tokenizer.index_word里面的数值,存在问题。再检查一下

C51 Flash 读写操作

发表于 2019-08-17
字数统计: 580 | 阅读时长 ≈ 2

C51 Flash 读写操作

###问题:下位机返回的数据,不是传感器的数据,有没有连接传感器,返回的数值也都一样。刚开始认为是标定的问题,把标定的函数搞明白,知道怎么去标定数据。但是标定完成后,数据还是不正确。

###思路:检查一下读取的Flash的驱动是不是有问题。写一个简单的函数,通过调用原来的读写函数,对flash里面的数值进行操作,每次下位机开断电,都会通过串口发送flash内部的数值,并每次把flash里的数据加一,然后再放到flash。简单的验证一下可以往flash内部进行数据的读写。

结果:每次返回的数值 不会发生变化。

最后可以试一试外部的存储芯片模块。。。。24C08

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

unsigned char xdata hc[3]={0,0,0};


//-----------------存储标定的k、b值--------------
void Save_flash(void)
{
uint8 i,j;
EA=0; //禁止中断
FLSCL=0x89; //FLASH 写/擦除定时(Fosc=18.432MHz) 允许FLASH写
PSCTL=0x07; //临时FLASH 允许FLASH 写/擦除
FLASH_ADDR[0]=0; //向待擦除扇区内的任何一个地址写入一个数据字节?擦除 临时FLASH
for(i=0;i<2;i++) //写入数据FLASH
{
PSCTL=0x00; //程序数据FLASH 禁止FLASH 写/擦除
j=hc[i]; //读FLASH存储器 与PSWE无关
PSCTL=0x05; //临时FLASH 禁止擦除 允许写
FLASH_ADDR[i]=j;
}
FLSCL=0x4f; //禁止FLASH 写/擦除
PSCTL=0x00; //程序数据FLASH 禁止FLASH 写/擦除
EA=1;
}
//-------------从MCU中读取出标定的k、b值------------------------
void Read_flash(void)
{
uint8 i,j;
uint8 code *FLASH_READ; //code定义的数据在flash中
FLASH_READ=0x0000;
for(i=0;i<2;i++)
{
PSCTL=0x04; //选择数据FLASH扇区 禁止FLASH 写/擦除
j=FLASH_READ[i]; //读取数据flash 0x0000区
PSCTL=0x00; //选择程序Flash 禁止写/擦除
hc[i]=j; //读程序flash
}
}


void main()
{

WDTCN = 0xde; // Disable watchdog timer 关闭看门狗
WDTCN = 0xad;
SYSCLK_Init();
PORT_Init();
ADC0_Init();
UART0_Init();

Read_All();
Para[0]++;
Save_Para();


P2 = 0x00;
EA = 1; // Enable global interrupts

Send_Buf0[0] = Para[0]; //发送帧头
Send_Buf0[1] = 0x00;
Send_Max = 2;
Send_p = 0;
SCON0 &= 0xef; //REN0 = 0 禁止开始接收
SBUF0 = 0x41; //启动回答数据事件

while(1)
{
WDTCN = 0xa5; //使能并复位看门狗定时器
}
}

原始对flash保存的代码,这里的Para变量是这么define的 uint8 xdata Para[112];

plot_attention

发表于 2019-08-16
字数统计: 62 | 阅读时长 ≈ 1

def plot_attention(attention_matrix, input_sentence, predicted_sentence):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1, 1, 1)

ax.matshow(attention_matrix, cmap='viridis') # cmap 定义的是配色

font_dict = {'fontsize': 14}

ax.set_xticklabels([''] + input_sentence, fontdict = font_dict, rotation = 90) # roation 让字体反转90度
ax.set_yticklabels([''] + predicted_sentence,fontdict = font_dict)
plt.show()
1…161718…25
Square Face

Square Face

少不吃苦是废人,老不吃苦是贵人

121 日志
3 分类
32 标签
GitHub E-Mail YouTube Instagram
© 2020 Square Face | Site words total count: 45.5k
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4