ここでは、以下のスペックのコンピュータを用いて比較します。
OS
Windows10
CPU
CPU Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
物理RAM
16.0GB
各フレームワークは、TensorFlow (2.1.0), PyTorch (1.7.0), Neural Designer (5.9.0)をインストールします。
以下のコードは、TensorFlow、Pytorchによる実装例です。
TensorFlow
#TENSORFLOW CODE
import tensorflow as tf
import pandas as pd
import time
import numpy as np
#read data float32
start_time = time.time()
filename = "C:/R_new.csv"
df_test = pd.read_csv(filename, nrows=100)
float_cols = [c for c in df_test if df_test[c].dtype == "float64"]
float32_cols = {c: np.float32 for c in float_cols}
data = pd.read_csv(filename, engine='c', dtype=float32_cols)
print("Loading time: ", round(time.time() - start_time), " seconds")
x = data.iloc[:,:-1].values
y = data.iloc[:,[-1]].values
initializer = tf.keras.initializers.RandomUniform(minval=-1., maxval=1.)
#build model
model = tf.keras.models.Sequential([tf.keras.layers.Dense(1000,
activation = 'tanh',
kernel_initializer = initializer,
bias_initializer=initializer),
tf.keras.layers.Dense(1,
activation = 'linear',
kernel_initializer = initializer,
bias_initializer=initializer)])
#compile model
model.compile(optimizer='adam', loss = 'mean_squared_error')
#train model
start_time = time.time()
history = model.fit(x, y, batch_size = 1000, epochs = 1000)
print("Training time: ", round(time.time() - start_time), " seconds")
PyTorchでこのアプリケーションを構築するには、いくつかのPythonスクリプトも必要です。
また、こちらからもコードを
ダウンロードできます。
PyTorch
#PYTORCH CODE
import pandas as pd
import time
import torch
import numpy as np
import statistics
def init_weights(m):
if type(m) == torch.nn.Linear:
torch.nn.init.uniform_(m.weight, a=-1.0, b=1.0)
torch.nn.init.uniform_(m.bias.data, a=-1.0, b=1.0)
epoch = 1000
total_samples, batch_size, input_variables, hidden_neurons, output_variables = 1000000, 1000, 1000, 1000, 1
device = torch.device("cuda:0")
# read data float32
start_time = time.time()
filename = "C:/R_new.csv"
df_test = pd.read_csv(filename, nrows=100)
float_cols = [c for c in df_test if df_test[c].dtype == "float64"]
float32_cols = {c: np.float32 for c in float_cols}
dataset = pd.read_csv(filename, engine='c', dtype=float32_cols)
print("Loading time: ", round(time.time() - start_time), " seconds")
x = torch.tensor(dataset.iloc[:,:-1].values, dtype = torch.float32)
y = torch.tensor(dataset.iloc[:,[-1]].values, dtype = torch.float32)
# build model
model = torch.nn.Sequential(torch.nn.Linear(input_variables, hidden_neurons),
torch.nn.Tanh(),
torch.nn.Linear(hidden_neurons, output_variables)).cuda()
# initialize weights
model.apply(init_weights)
# compile model
learning_rate = 0.001
loss_fn = torch.nn.MSELoss(reduction = 'mean')
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
indices = np.arange(0,total_samples)
start = time.time()
for j in range(epoch):
mse=[]
t0 = time.time()
for i in range(0, total_samples, batch_size):
batch_indices = indices[i:i+batch_size]
batch_x, batch_y = x[batch_indices], y[batch_indices]
batch_x = batch_x.cuda()
batch_y = batch_y.cuda()
outputs = model.forward(batch_x)
loss = loss_fn(outputs, batch_y)
model.zero_grad()
loss.backward()
optimizer.step()
mse.append(loss.item())
print("Epoch:", j+1,"/1000", "[================================] - ","loss: ", statistics.mean(mse))
t1 = time.time() - t0
print("Elapsed time: ", int(round(t1 )), "sec")
end = time.time()
elapsed = end - start
print("Training time: ",int(round(elapsed )), "seconds")
TensorFlow、PyTorch、Neural Designerでそれぞれ実装できたら実行します。