tensorflow改寫為pytorch的方法總結

2021-09-25 07:51:52 字數 2230 閱讀 2732

1.matrix = tf.get_variable("matrix", [output_size, input_size], dtype=input_.dtype)

=matrix = variable(torch.randn(output_size, input_size))

2.self.d_l2_loss = tf.constant(0.0)

=self.d_l2_loss = torch.tensor(0.0)

3.losses = tf.nn.softmax_cross_entropy_with_logits(logits=d_scores, labels=self.d_input_y)

=losses = torch.nn.crossentropyloss(d_scores, d_input_y)

4.tf.reduce_mean(losses)

=torch.mean(losses)

5.embedded_chars = tf.nn.embedding_lookup(w_fe, feature_input + 1)

=embedded_chars = torch.index_select(w_fe, 0, feature_input + 1)

6.embedded_chars_expanded = tf.expand_dims(embedded_chars, -1)

=embedded_chars_expanded = torch.unsqueeze(embedded_chars, -1)

7.h_pool = tf.concat(pooled_outputs, 3)

=h_pool = torch.cat(pooled_outputs, 3)

8.tf.transpose(matrix)

=matrix.transpose(1, 0)

9.tensorarray可以看做是具有動態size功能的tensor陣列。通常都是跟while_loop或map_fn結合使用。

10.tf.tensorarray簡單教程    

ta.stack(name=none) 將tensorarray中元素疊起來當做乙個tensor輸出

ta.unstack(value, name=none) 可以看做是stack的反操作,輸入tensor,輸出乙個新的tensorarray物件

ta.write(index, value, name=none) 指定index位置寫入tensor

ta.read(index, name=none) 讀取指定index位置的tensor

11.torch.squeeze

=?????不確定

ta.stack

12.torch.matmul

=tf.matmul

13.tf.expand_dims

=torch.unsqueeze

14.torch.split( torch.cat([torch.split(input_x, i, 1)[0], self.padding_array], 1),

self.sequence_length, 1)[0]

=有一點不一樣

tf.split(tf.concat([tf.split(input_x, [i, self.sequence_length - i], 1)[0], self.padding_array], 1),[self.sequence_length, i], 1)[0]

14.tf.squeeze()

=torch.squeeze

16,sub_goal = tf.nn.l2_normalize(sub_goal, 1)

=sub_goal = sklearn.preprocessing.normalize(sub_goal , norm='l2')

17.tf.log(tf.nn.softmax()

=f.log_softmax()

18.tf.multinomial(log_prob, 1)

=torch.multinomial(log_prob, 1,  replacement=true)

19.tf.multiply

=*或者torch.mul

20.tf.one_hot

=???

onehot_encoder = onehotencoder(sparse=false)

onehot_encoder.fit_transform(next_token)

21.tf.reduce_sum(a, 0)

=a.sum(0)

改寫Minus語法為not exist

原sql select from select distinct v.parent id parentvehid,fvp.seat seat from fc vehicle v,fc vehicle parentfvp where v.parent id fvp.parent veh id minu...

sql not in 的坑及改寫為join

我們接著用上次建的兩個表。資料如下 sql select from l str v left 1 1 left 2 2 left 3 3 left 4 4 sql select from r str v right 3 3 right 4 4 right 5 5 right 6 6 如果要返回l表中...

wx 打包 py 為 exe 檔案

需要安裝pyinstaller pip install pyinstaller 把要打包的python檔案放到乙個沒有中文的路徑下,並且將字尾名.py改為.pyw。以後只要雙擊該檔案就能開啟圖形化,但是前提是要有python環境,所以要將它變為.exe可執行檔案才能在windows系統裡沒有pyth...