keras/pytorch/pandas開發(fā)代碼日志隨筆

torch代碼相關(guān)

assert 加judge 判斷式

tensor.item() 取一個(gè)元素

tensor.index_select() tensor行列位置選擇

model.state_dict(path) 從保存model的w、input-x等tensor,按照dict保存

對(duì)應(yīng)的->model.load_state_dict(path)加載模型




torch 的train和eval相關(guān)

model.eval() 測(cè)試模式,主要針對(duì)dropout batch_normalizetion

model.train()訓(xùn)練模式

model.eval 和 model.train 是訓(xùn)練模式和測(cè)試模式的切換

loss.backforwad()

loss 其實(shí)是一個(gè)函數(shù),model.forward的layer,所以有反向傳播的功能

optimizer.step()

梯度下降的調(diào)度

optimizer.step 和 loss.backforwad 都是一個(gè)step調(diào)度的,也就是1個(gè)batch

optimizer.step()

clip_grad_norm 是正則化,防止過擬合的

torch分布式

torch.nn.parallel.DistributedDataParallel(model.cuda())

torch相關(guān)-tensor:

tensor.contiguous().view(-1, self.output_dim)(這是flatten,也就是維度重組)

如果是tf就是:

tf.reshape(tensor, [-1, self.output_dim])(這也是flatten)

torch相關(guān)的調(diào)用主體一般是直接的tensor;而torch的調(diào)用主體一般就是tf了.

***用法請(qǐng)注意:

torch的主體一般是tensor;tensor模式

tensorflow的主題一般就是tf,圖模式

另外,view的參數(shù)x.view(0,index)相當(dāng)于x[index],(2,index)相當(dāng)于x【;,;,index】

而對(duì)應(yīng)的tensorflow的參數(shù)是tf.reshape(x,[0,index]),也就是有一個(gè)結(jié)論:?

tf的[]和torch的()是差不多的

tensor初始化:

torch.LongTensor(batch_size,max_len).fill_(value)

填充特定value:0,1,2都行

tensor.size() <==> tf.get_shape()

也可以這么使用:tensor.size(0)

tensor.cuda() 加載到gpu.

torch的layer函數(shù)與keras的layer函數(shù)的設(shè)計(jì)是相似的,都是layer(param)(x,y),seq只是將layer封裝入一個(gè)sequence類的方法,用法就是,seq(x,y)把layer的param都封裝進(jìn)去了,too simple

layer

torch 與 keras 的layer的習(xí)慣差不多

torch的api是nn, 相對(duì)的keras的api是layer, 都是用第一個(gè)()做init, 第一個(gè)()做train或者預(yù)測(cè)

torch 及 tensorflow device相關(guān):

其一:torch.cuda.set_device(gpu_use)

然后再用variabel.cuda()

其二:torch.tensor.to(device對(duì)象)

device = torch.device("cuda")

如果是tensorflow的話:

?1. conf = tf.ConfigProto(device_count={'GPU': 0})

? tf.enable_eager_execution(config=conf)

?2. with tf.device(/cpu:0):(tf也有device對(duì)象) 但是不像torch一樣有cuda對(duì)象

keras 自己封裝model

from keras.engine.topology import Layer

class MineModel(Layer):

new_model = multi_gpu_model(model, gpus=len(os.environ["CUDA_VISIBLE_DEVICES"].split(',')))

pandas相關(guān):

?dataframe是可以通過列的numpy或list賦值的

?舉例說明: pd_array['id'] = id_list

有關(guān)鏈接:

keras

http://www.itdecent.cn/p/25e30055d7ac?

https://keras-cn.readthedocs.io/en/latest/models/model/

http://www.itdecent.cn/p/b9ad6b26e690

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容