torch、tensorflow1 GPU测速代码python3(MNIST,简易版)

1、torch

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
import time

# print start time
start = time.time()
print("Start time = " + time.ctime())

# read data
inp = np.random.rand(12225, 9)

oup = np.random.rand(12225, 1)
#inp = inp*[4,100,1,4,0.04,1]
oup = oup * 500
inp = inp.astype(np.float32)
oup = oup.astype(np.float32)
# Hyper Parameters
input_size = inp.shape[1]
hidden_size = 10
output_size = 1
num_epochs = 1000
learning_rate = 0.001

# Toy Dataset
x_train = inp
y_train = oup

device = torch.device("cuda:0")


# Linear Regression Model
class Net(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.l1 = nn.ReLU()
        self.l2 = nn.Sigmoid()
        self.l3 = nn.Tanh()
        self.l4 = nn.ELU()
        self.l5 = nn.Hardshrink()
        self.ln = nn.Linear(hidden_size, hidden_size)
        self.fc2 = nn.Linear(hidden_size, output_size)
        # self.fc1 = nn.Linear(input_size, output_size)

    def forward(self, x):
        out = self.fc1(x)
        out = self.l3(out)
        out = self.ln(out)
        out = self.l1(out)
        out = self.fc2(out)
        return out


model = Net(input_size, hidden_size, output_size)

# Loss and Optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

###### GPU
if torch.cuda.is_available():
    print("We are using GPU now!!!")
    model = model.to(device)

# Train the Model
for epoch in range(num_epochs):
    # Convert numpy array to torch Variable
    if torch.cuda.is_available():
        inputs = Variable(torch.from_numpy(x_train).to(device))
        targets = Variable(torch.from_numpy(y_train).to(device))
    else:
        inputs = Variable(torch.from_numpy(x_train))
        targets = Variable(torch.from_numpy(y_train))

    # Forward + Backward + Optimize
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()

    if (epoch + 1) % 100 == 1:
        print('Epoch [%d/%d], Loss: %.4f' % (epoch + 1, num_epochs, loss.data))

# print end time
end = time.time()
print("End time = " + time.ctime())
print("time cost: " + str(end - start))

# Plot the graph
if torch.cuda.is_available():
    predicted = model(Variable(
        torch.from_numpy(x_train).to(device))).data.cpu().numpy()
else:
    predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
# plt.plot(y_train / 500, 'r-', label='Original data')
# plt.plot(predicted / 500, '-', label='Fitted line')
# #plt.plot(y_train/500, predicted/500,'.', label='Fitted line')
# plt.legend()
# plt.show()

# Save the Model
# torch.save(model.state_dict(), 'model.pkl')

2、tensorflow1

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import time

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_actual = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784, 10]))  #初始化权值W
b = tf.Variable(tf.zeros([10]))  #初始化偏置项b
y_predict = tf.nn.softmax(tf.matmul(x, W) + b)  #加权变换并进行softmax回归,得到预测概率
cross_entropy = tf.reduce_mean(
    -tf.reduce_sum(y_actual * tf.log(y_predict), reduction_indices=1))  #求交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
    cross_entropy)  #用梯度下降法使得残差最小

correct_prediction = tf.equal(tf.argmax(y_predict, 1),
                              tf.argmax(y_actual, 1))  #在测试阶段,测试准确度计算
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))  #多个批次的准确度均值

init = tf.initialize_all_variables()
start = time.time()
with tf.Session() as sess:
    sess.run(init)
    for i in range(10000):  #训练阶段,迭代1000次
        batch_xs, batch_ys = mnist.train.next_batch(100)  #按批次训练,每批100行数据
        sess.run(train_step, feed_dict={
            x: batch_xs,
            y_actual: batch_ys
        })  #执行训练
        if (i % 1000 == 0):  #每训练100次,测试一次
            print(
                "accuracy:",
                sess.run(accuracy,
                         feed_dict={
                             x: mnist.test.images,
                             y_actual: mnist.test.labels
                         }))

end = time.time()
print("time cost: " + str(end - start))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值