日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Show Attend and Tell的实现代码中的python知识学习

發(fā)布時間:2024/10/6 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Show Attend and Tell的实现代码中的python知识学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Collection的類Counter()

參考:collections模塊—— Counter

from collections import Counter c = Counter('gallahad') print(c['l']) 輸出:2

collections之Counter對象的update方法

參考:collections之Counter對象的update方法
構(gòu)造一個 Counter對象

""" 構(gòu)造一個空 Counter 對象 """ # c = Counter() # a new, empty counter""" 三種構(gòu)造非空 Counter 對象的方法,返回結(jié)果的順序與輸入順序相同 """ # 1、a new counter from an iterable # c = Counter('bccabc') # Counter({'b': 2, 'c': 3, 'a': 1}) # 2、a new counter from a mapping # c = Counter({'a': 1, 'b': 2, 'c': 3}) # Counter({'a': 1, 'b': 2, 'c': 3}) # 3、a new counter from keyword args c = Counter(a=1, b=2, c=3) # Counter({'a': 1, 'b': 2, 'c': 3})

執(zhí)行 update 方法:update([iterable-or-mapping])

""" update 后的參數(shù)可以是:可迭代對象 或者 映射 操作原理:如果要更新的關(guān)鍵字已存在,則對它的值進(jìn)行求和;如果不存在,則添加 """ # 1、可迭代對象:字符串 c.update("baaac") c # Out[118]: Counter({'a': 4, 'b': 3, 'c': 4})# 2、映射:字典 c.update({"d":-2, c['a']:-1}) # 因為 a=4, 所以結(jié)果中顯示 4: -1 c # Out[128]: Counter({'a': 4, 'b': 3, 'c': 4, 'd': -2, 4: -1})# 3、映射:字典的另一種表現(xiàn)形式 c.update(d=3, a=-1) c # Out[130]: Counter({'a': 3, 'b': 3, 'c': 4, 'd': 1, 4: -1}) from collections import Counter c = Counter('gallahad') print(c) print(c['l']) c.update(['a','v','p','l','k','j']) print(c)

輸出

Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1}) 2 Counter({'a': 4, 'l': 3, 'g': 1, 'h': 1, 'd': 1, 'v': 1, 'p': 1, 'k': 1, 'j': 1})

文件:dataset_flickr8k.json的格式

這是一個json文件,里面只有兩個鍵,如下

{"images": [{***},{***},{***},...,{***}], "dataset": "flickr8k"}

一共有8000個,這個數(shù)是看"imgid"的值的,"imgid"從0到7999,代表每張圖片。
1個{***}里面的內(nèi)容是

{"sentids": [0, 1, 2, 3, 4],"imgid": 0, "sentences": [{¥¥¥},{¥¥¥},{¥¥¥},{¥¥¥},{¥¥¥}],"split": "train","filename": "2513260012_03d33305cf.jpg" }

1個{¥¥¥}里面的內(nèi)容是

{"tokens": ["a", "black", "dog", "is", "running", "after", "a", "white", "dog", "in", "the", "snow"],"raw": "A black dog is running after a white dog in the snow .","imgid": 0,"sentid": 0}, {"tokens": ["black", "dog", "chasing", "brown", "dog", "through", "snow"],"raw": "Black dog chasing brown dog through snow","imgid": 0,"sentid": 1}, {"tokens": ["two", "dogs", "chase", "each", "other", "across", "the", "snowy", "ground"],"raw": "Two dogs chase each other across the snowy ground .","imgid": 0,"sentid": 2}, {"tokens": ["two", "dogs", "play", "together", "in", "the", "snow"],"raw": "Two dogs play together in the snow .","imgid": 0,"sentid": 3}, {"tokens": ["two", "dogs", "running", "through", "a", "low", "lying", "body", "of", "water"],"raw": "Two dogs running through a low lying body of water .","imgid": 0,"sentid": 4}

一共有5個,代表每張圖片有5句描述。

python中的enumerate()索引在前,元素在后

words = ['a','v','p','l','k','j'] word_map = {k: v + 1 for v, k in enumerate(words)} print(word_map)for v, k in enumerate(words):print(v,k)

輸出

{'a': 1, 'v': 2, 'p': 3, 'l': 4, 'k': 5, 'j': 6} 0 a 1 v 2 p 3 l 4 k 5 j

Python-json.dumps()和dump()、json.loads()和load()的區(qū)別

參考:Python-json.dumps()和dump()、json.loads()和load()的區(qū)別
主要區(qū)別:
1、dump(),load() 處理的是json文件
2、dumps(),loads() 處理的是字符串

1、json.dumps()是將字典類型轉(zhuǎn)化成字符串類型

dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} str = json.dumps(dic) print(type(str)) print(str)

運行結(jié)果:

<class 'str'> {"code": 0, "msg": "success", "agentid": "1000021", "state": "fpp_unify_auth"}

2、json.loads()將字符串類型轉(zhuǎn)化成字典類型

dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} strs = json.dumps(dic) dics = json.loads(strs) print(type(dics)) print(dics)

運行結(jié)果:

<class 'dict'> {'code': 0, 'msg': 'success', 'agentid': '1000021', 'state': 'fpp_unify_auth'}

3、json.dump()用于將dict類型的數(shù)據(jù)轉(zhuǎn)成str,并寫入到j(luò)son文件中

dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} json_filename = 'test_json.json' json.dump(dic, open(json_filename, "w"))

運行結(jié)果:

生成test_json.json文件,并寫入內(nèi)容{"code": 0, "msg": "success", "agentid": "1000021", "state": "fpp_unify_auth"}

4、json.load()用于從json文件中讀取數(shù)據(jù)

json_filename = 'test_json.json' js = json.load(open(json_filename)) print(type(js)) print(js)

運行結(jié)果:

<class 'dict'> {'code': 0, 'msg': 'success', 'agentid': '1000021', 'state': 'fpp_unify_auth'}

for循環(huán)

train_image_paths = [] train_image_captions = []val_image_paths = [] val_image_captions = []test_image_paths = [] test_image_captions = []for impaths, imcaps, split in [(train_image_paths, train_image_captions, 'TRAIN'),(val_image_paths, val_image_captions, 'VAL'),(test_image_paths, test_image_captions, 'TEST')]:print(impaths, imcaps, split)

輸出結(jié)果:

[] [] TRAIN [] [] VAL [] [] TEST

這樣輸出的結(jié)果解釋:本質(zhì)還是對列表進(jìn)行循環(huán),不能被三個變量給嚇住了,每循環(huán)列表中的一個元素,這三個變量就會進(jìn)行相應(yīng)的對應(yīng),這樣就會得到相應(yīng)的值。

python庫 h5py入門講解

參考:python庫——h5py入門講解
h5py簡單介紹
h5py文件是存放兩類對象的容器,數(shù)據(jù)集(dataset)和組(group),dataset類似數(shù)組類的數(shù)據(jù)集合,和numpy的數(shù)組差不多。group是像文件夾一樣的容器,它好比python中的字典,有鍵(key)和值(value)。group中可以存放dataset或者其他的group?!辨I”就是組成員的名稱,”值”就是組成員對象本身(組或者數(shù)據(jù)集)
圖片參考:h5py 必知–String存儲

random.choice()

是從序列中獲取一個隨機(jī)元素,參考:python:random.choice()方法

import random print(random.choice("I am very help"))

輸出結(jié)果:

o

tqdm進(jìn)度條 在pytorch中的使用

參考:tqdm進(jìn)度條 在pytorch中的使用
引入該模塊:

from tqdm import tqdm

tqdm可用于所有可迭代對象,所以可以直接用在dataloader上,如下:

for data, target in tqdm(train_loader): ...

效果:

100%|██████████| 1000/1000 [00:27<00:00, 35.91it/s]

tqdm用在dataloader上其實是對每個batch和batch總數(shù)做的進(jìn)度條。

采樣

參考:py001- random中sample()函數(shù)的用法
sample(list, k)返回一個長度為k新列表,新列表存放list所產(chǎn)生k個隨機(jī)唯一的元素
函數(shù)用法實例:

import randomlist = [1, 2, 3] print(random.sample(list ,2))list = ["china","python","sky"] print(random.sample(list ,2))list = range(1, 10000) print(random.sample(list ,5))

輸出:

[1, 2] ['python', 'sky'] [6912, 1869, 5991, 721, 3388]

運行

from random import sample imcaps = [["a", "black", "dog", "is", "running", "after", "a", "white", "dog", "in", "the", "snow"],["black", "dog", "chasing", "brown", "dog", "through", "snow"],["two", "dogs", "chase", "each", "other", "across", "the", "snowy", "ground"],["two", "dogs", "play", "together", "in", "the", "snow"],["two", "dogs", "running", "through", "a", "low", "lying", "body", "of", "water"]] captions = sample(imcaps , k=5) print(captions)

輸出結(jié)果

[['two', 'dogs', 'play', 'together', 'in', 'the', 'snow'], ['a', 'black', 'dog', 'is', 'running', 'after', 'a', 'white', 'dog', 'in', 'the', 'snow'], ['two', 'dogs', 'running', 'through', 'a', 'low', 'lying', 'body', 'of', 'water'], ['black', 'dog', 'chasing', 'brown', 'dog', 'through', 'snow'], ['two', 'dogs', 'chase', 'each', 'other', 'across', 'the', 'snowy', 'ground']]

讀取圖片

from imageio import imread img = imread(r'C:\Users\***\Desktop\20210617173058874.png') print(img.shape) print(len(img.shape))

輸出

(1134, 1143, 4) 3

參考:OpenCV學(xué)習(xí)筆記(7)圖像的通道(channels)問題
通道數(shù)
整理一下OpenCV中文論壇里關(guān)于圖像通道的問題,如下:

(1)圖像的通道指的是什么?是不是灰度圖的通道數(shù)為1,彩色圖的通道為3?(zhuker)

正確!
基本上,描述一個像素點,如果是灰度,那么只需要一個數(shù)值來描述它,就是單通道。
如果一個像素點,有RGB三種顏色來描述它,就是三通道。(ollydbg23)

(2)對于2通道和4通道如何看待?哪位幫忙解釋一下?(feixue)

我見過四通道的,兩通道暫時沒見過,估計只是編程的方便吧。windows的bmp有時候是一個四通道圖像,R、G、B加上一個A通道,表示透明度。(eralvc)

是的,最后這個,一般叫做alpha通道,表示透明度的。(ollydbg23)

4通道通常為RGBA,在某些處理中可能會用到。
2通道圖像不常見,通常在程序處理中會用到,如傅里葉變換,可能會用到,一個通道為實數(shù),一個通道為虛數(shù),主要是編程方便。
還有一種情況就是16位圖像,本來是3通道,但是為了減少數(shù)據(jù)量,壓縮為16位,剛好兩個通道,常見格式有RGB555或RGB565,也就是說R占5位,G占5或6位,B占5位,也有RGBA5551格式。古老的格式,不用也罷。(Loren)

主要是有些攝像頭常采用一些比較“古怪”的格式,沒辦法。
補(bǔ)充一種情況,目前常見的一些攝像頭喜歡采用YUV2等格式,格式如下YUYV,在處理的時候可以用4通道或者2通道來處理。
如原格式為:Y1UY2V,插值成為Y1UV,Y2UV 就成兩個彩色點了。
YCrCb也有類似壓縮情況。(Loren)

于np.newaxis的一點理解

import numpy as np a = np.arange(0, 10) print(a) print(a.shape) [0 1 2 3 4 5 6 7 8 9] (10,)

那么我如何才能將其轉(zhuǎn)化為shape=(1,10)呢
可以用兩種方法:

1.使用shape

import numpy as np a = np.arange(0, 10) print(a) a.shape=(10,1) print(a) [0 1 2 3 4 5 6 7 8 9] [[0][1][2][3][4][5][6][7][8][9]]

2. 使用np.newaxis

import numpy as np a = np.arange(0, 10) print(a) a = a[:,np.newaxis] print(a)

結(jié)果如下:

[0 1 2 3 4 5 6 7 8 9] [[0][1][2][3][4][5][6][7][8][9]]

Numpy中numpy.concatenate()函數(shù)

參考:
1、Numpy中numpy.concatenate()函數(shù)、numpy.delete()函數(shù)、numpy.mean()函數(shù)參數(shù)axis=0與axis=1的區(qū)分

參數(shù)axis=0,1,2

2、Python學(xué)習(xí)筆記——參數(shù)axis=0,1,2…

scipy.misc.imresize

參考:
1、python–scipy.misc.imresize()函數(shù)
2、scipy筆記—scipy.misc.imresize用法(方便訓(xùn)練圖像數(shù)據(jù))
3、scipy.misc.imresize改變圖像的大小
重新設(shè)置圖像像素

Numpy中transpose()函數(shù)的可視化理解

參考:Numpy中transpose()函數(shù)的可視化理解

cudnn.benchmark = True?是什么意思

參考:cudnn.benchmark = True?是什么意思

總結(jié)

以上是生活随笔為你收集整理的Show Attend and Tell的实现代码中的python知识学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。