使用GPU运行python代码

时间:2018-08-01 09:54:06

标签: python-3.x tensorflow neural-network gpu

耶拿气候法规如下

import numpy as np
import os
from matplotlib import pyplot as plt
from numba import vectorize
f=open('jena.csv')
data=f.read()
f.close()
lines=data.split('\n')
header=lines[0].split(',')
lines=lines[1:]
print(header)
N=len(lines)
print(N)
float_data=np.zeros((len(lines),len(header)-1))
for i, line in enumerate(lines):
    values=[float(x) for x in line.split(',')[1:]]
    float_data[i,:]=values
mean=float_data[:200000].mean(axis=0)
float_data -=mean
std=float_data[:200000].std(axis=0)
float_data/=std

def generator(data,lookback,delay,min_index,max_index,shuffle=False,batch_size=128,step=6):
    if max_index is None:
        max_index=len(data)-delay-1
    i=min_index+lookback
    while 1:
        if shuffle:
            rows=np.random.randint(
                min_index+lookback,max_index,size=batch_size)
        else:
            if i + batch_size>=max_index:
                i=min_index+lookback
            rows=np.arange(i,min(i+batch_size,max_index))
            i+=len(rows)
        samples=np.zeros((len(rows),lookback//step,data.shape[-1]))
        targets=np.zeros((len(rows),))
        for j, row in enumerate(rows):
            indices=range(rows[j]-lookback,rows[j],step)
            samples[j]=data[indices]
            targets[j]=data[rows[j]+delay][1]
        yield samples, targets
lookback=1440
step=6
delay=144
batch_size=128
train_gen=generator(float_data,lookback=lookback,delay=delay,min_index=0,max_index=200000,shuffle=True,step=step,batch_size=batch_size)
val_gen=generator(float_data,lookback=lookback,delay=delay,min_index=200001,max_index=300000,step=step,batch_size=batch_size)
test_gen=generator(float_data,lookback=lookback,delay=delay,min_index=300001,max_index=None,step=step,batch_size=batch_size)
val_steps=(300000-200001-lookback)
test_steps=(len(float_data)-300001-lookback)
def evaluate_naive_method():
    batch_maes=[]
    for step in range(val_steps):
        samples,targets=next(val_gen)
        mae=np.mean(np.abs(preds-targets))
        batch_maes.append(mae)
    print(np.mean(batch_maes))
evaluate_naive_method()

当我执行代码时,它使用CPU并花费大约14分钟来产生mae。 我想在本节中使用GPU使用Tensorflow,以便输出可以更快。

for step in range(val_steps):
        samples,targets=next(val_gen)
        mae=np.mean(np.abs(preds-targets))
        batch_maes.append(mae)

我应该将变量“ samples”和“ targets”转换为张量流,以便更快地获得输出吗?如果是这样,我如何将其转换为张量流?

1 个答案:

答案 0 :(得分:0)

Tensorflow可以满足您的要求,请使用下面的示例使用GPU

https://www.tensorflow.org/guide/using_gpuhttps://www.tensorflow.org/guide/using_gpu