12 Commits

Author SHA1 Message Date
Shua Dissen 804bf1dd8d estimator inference files 2020-05-23 18:47:01 +03:00
Shua Dissen ee0fa21e21 Add files via upload 2020-05-10 19:04:40 +03:00
Shua Dissen 11e1752c4f Create models 2020-05-10 19:02:55 +03:00
Shua Dissen a4709194cb Delete a 2020-05-10 19:01:10 +03:00
Shua Dissen aba3a50428 Delete LPC_NN.pt 2020-05-10 19:00:56 +03:00
Shua Dissen 2dc54c791d Create a 2020-05-10 19:00:26 +03:00
Shua Dissen 3f77a9352f Add files via upload 2020-05-10 18:58:12 +03:00
Shua Dissen 764680163c Add files via upload 2020-05-09 17:53:30 +03:00
Joseph Keshet bdb36bc4a4 Correct formatting 2020-04-29 22:07:15 +03:00
Joseph Keshet b0df9d73b1 Update Google drive link to the tracking model 2020-04-29 22:04:52 +03:00
Shua Dissen 34e764bbcf Add files via upload 2018-12-02 16:12:13 +02:00
Shua Dissen a8360af25c numpy vs 1.14 fix 2018-01-24 15:03:06 +02:00
15 changed files with 904 additions and 619 deletions
+246
View File
@@ -0,0 +1,246 @@
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import wave
import os
import math
from scipy.fftpack.realtransforms import dct
from copy import deepcopy
from scipy.fftpack import fft, ifft
from scikits.talkbox.linpred import lpc
np.random.seed(1337)
epsilon = 0.0000000001
def build_data(wav, begin=None, end=None):
wav_in_file = wave.Wave_read(wav)
wav_in_num_samples = wav_in_file.getnframes()
N = wav_in_file.getnframes()
dstr = wav_in_file.readframes(N)
data = np.fromstring(dstr, np.int16)
return data
def periodogram(x, nfft=None, fs=1):
"""Compute the periodogram of the given signal, with the given fft size.
Parameters
----------
x : array-like
input signal
nfft : int
size of the fft to compute the periodogram. If None (default), the
length of the signal is used. if nfft > n, the signal is 0 padded.
fs : float
Sampling rate. By default, is 1 (normalized frequency. e.g. 0.5 is the
Nyquist limit).
Returns
-------
pxx : array-like
The psd estimate.
fgrid : array-like
Frequency grid over which the periodogram was estimated.
Examples
--------
Generate a signal with two sinusoids, and compute its periodogram:
>>> fs = 1000
>>> x = np.sin(2 * np.pi * 0.1 * fs * np.linspace(0, 0.5, 0.5*fs))
>>> x += np.sin(2 * np.pi * 0.2 * fs * np.linspace(0, 0.5, 0.5*fs))
>>> px, fx = periodogram(x, 512, fs)
Notes
-----
Only real signals supported for now.
Returns the one-sided version of the periodogram.
Discrepency with matlab: matlab compute the psd in unit of power / radian /
sample, and we compute the psd in unit of power / sample: to get the same
result as matlab, just multiply the result from talkbox by 2pi"""
x = np.atleast_1d(x)
n = x.size
if x.ndim > 1:
raise ValueError("Only rank 1 input supported for now.")
if not np.isrealobj(x):
raise ValueError("Only real input supported for now.")
if not nfft:
nfft = n
if nfft < n:
raise ValueError("nfft < signal size not supported yet")
pxx = np.abs(fft(x, nfft)) ** 2
if nfft % 2 == 0:
pn = nfft / 2 + 1
else:
pn = (nfft + 1) / 2
fgrid = np.linspace(0, fs * 0.5, pn)
return pxx[:pn] / (n * fs), fgrid
def arspec(x, order, nfft=None, fs=1):
"""Compute the spectral density using an AR model.
An AR model of the signal is estimated through the Yule-Walker equations;
the estimated AR coefficient are then used to compute the spectrum, which
can be computed explicitely for AR models.
Parameters
----------
x : array-like
input signal
order : int
Order of the LPC computation.
nfft : int
size of the fft to compute the periodogram. If None (default), the
length of the signal is used. if nfft > n, the signal is 0 padded.
fs : float
Sampling rate. By default, is 1 (normalized frequency. e.g. 0.5 is the
Nyquist limit).
Returns
-------
pxx : array-like
The psd estimate.
fgrid : array-like
Frequency grid over which the periodogram was estimated.
"""
x = np.atleast_1d(x)
n = x.size
if x.ndim > 1:
raise ValueError("Only rank 1 input supported for now.")
if not np.isrealobj(x):
raise ValueError("Only real input supported for now.")
if not nfft:
nfft = n
a, e, k = lpc(x, order)
# This is not enough to deal correctly with even/odd size
if nfft % 2 == 0:
pn = nfft / 2 + 1
else:
pn = (nfft + 1) / 2
px = 1 / np.fft.fft(a, nfft)[:pn]
pxx = np.real(np.conj(px) * px)
pxx /= fs / e
fx = np.linspace(0, fs * 0.5, pxx.size)
return pxx, fx
def arspecs(input_wav, order, Atal=False):
epsilon = 0.0000000001
data = input_wav
ar = []
ars = arspec(data, order, 4096)
for k, l in zip(ars[0], ars[1]):
ar.append(math.log(math.sqrt((k ** 2) + (l ** 2))))
for val in range(0, len(ar)):
if ar[val] == 0.0:
ar[val] = deepcopy(epsilon)
mspec1 = np.log10(ar)
# Use the DCT to 'compress' the coefficients (spectrum -> cepstrum domain)
ar = dct(mspec1, type=2, norm='ortho', axis=-1)
return ar[:30]
def specPS(input_wav, pitch):
N = len(input_wav)
samps = N / pitch
if samps == 0:
samps = 1
frames = N / samps
data = input_wav[0:frames]
specs = periodogram(data, nfft=4096)
for i in range(1, int(samps)):
data = input_wav[frames * i:frames * (i + 1)]
peri = periodogram(data, nfft=4096)
for sp in range(len(peri[0])):
specs[0][sp] += peri[0][sp]
for s in range(len(specs[0])):
specs[0][s] /= float(samps)
peri = []
for k, l in zip(specs[0], specs[1]):
if k == 0 and l == 0:
peri.append(epsilon)
else:
peri.append(math.log(math.sqrt((k ** 2) + (l ** 2))))
# Filter the spectrum through the triangle filterbank
mspec = np.log10(peri)
# Use the DCT to 'compress' the coefficients (spectrum -> cepstrum domain)
ceps = dct(mspec, type=2, norm='ortho', axis=-1)
return ceps[:50]
def build_single_feature_row(data):
lpcs = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
arr = []
periodo = specPS(data, 50)
arr.extend(periodo)
for j in lpcs:
ars = arspecs(data, j)
arr.extend(ars)
for i in range(len(arr)):
if np.isnan(np.float(arr[i])):
arr[i] = 0.0
return arr
def get_y():
data = np.load('timit.npy')
timit = []
for row in data:
y = open('Y/' + str(row[0]).replace("timit", "VTRFormants") + ".y").readline().split()
arr = []
arr.append(float(y[0]))
arr.append(float(y[1]))
arr.append(float(y[2]))
arr.append(float(y[3]))
arr.extend(row)
timit.append(arr)
nump = np.asarray(timit)
np.save('timit_train_arspec',nump)
return
def build_timit_data():
arcep_mat = []
path = 'X_test/'
for file in [f for f in os.listdir(path) if f.endswith('.wav')]:
name = file.replace('.wav', '')
y = open('Y_test' + '/' + str(name).replace("timit", "VTRFormants") + ".y").readline().split()
X = build_data(path + file)
arr = [name]
arr.append(float(y[0]))
arr.append(float(y[1]))
arr.append(float(y[2]))
arr.append(float(y[3]))
arr.extend(build_single_feature_row(X))
arcep_mat.append(arr)
nump = np.asarray(arcep_mat)
np.save('timitTest',nump)
arcep_mat = []
path = 'X/'
for file in [f for f in os.listdir(path) if f.endswith('.wav')]:
name = file.replace('.wav', '')
y = open('Y/' + str(name).replace("timit", "VTRFormants") + ".y").readline().split()
X = build_data(path + file)
arr = [name]
arr.append(float(y[0]))
arr.append(float(y[1]))
arr.append(float(y[2]))
arr.append(float(y[3]))
arr.extend(build_single_feature_row(X))
arcep_mat.append(arr)
nump = np.asarray(arcep_mat)
np.save('timitTrain',nump)
return
build_timit_data()
+135
View File
@@ -0,0 +1,135 @@
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
train_data = np.load("timitTrain.npy")
test_data = np.load("timitTest.npy")
Xtrain = train_data[:,5:].astype(np.float32)
Ytrain = train_data[:,1:5].astype(np.float32)
Xtest = test_data[:,5:].astype(np.float32)
Ytest = test_data[:,1:5].astype(np.float32)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
_, D = Xtrain.shape
K = len(Ytrain)
print(D, K)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.Dense1 = nn.Linear(D, 1024)
self.Dense2 = nn.Linear(1024, 512)
self.Dense3 = nn.Linear(512, 256)
self.out = nn.Linear(256, 4)
def forward(self, x):
x = torch.sigmoid(self.Dense1(x))
x = torch.sigmoid(self.Dense2(x))
x = torch.sigmoid(self.Dense3(x))
return self.out(x)
loss = nn.L1Loss()
def train(model, loss, optimizer, inputs, labels):
inputs = Variable(inputs.to(device))
labels = Variable(labels.to(device))
optimizer.zero_grad()
logits = model.forward(inputs)
output = loss.forward(logits, labels)
output.backward()
optimizer.step()
return output.item()
def predict(model, inputs):
inputs = Variable(inputs)
logits = model.forward(inputs.to(device))
return logits.data.cpu().numpy()
torch.manual_seed(0)
Xtrain = torch.from_numpy(Xtrain).float().to(device)
Ytrain = torch.from_numpy(Ytrain).float().to(device)
Xtest = torch.from_numpy(Xtest).float().to(device)
Ytest = torch.from_numpy(Ytest).float().to(device)
model = Net().to(device)
optimizer = optim.Adagrad(model.parameters(), lr=0.01)
epochs = 80
batchSize = 20
n_batches = Xtrain.size()[0]
costs = []
test_accuracies = []
print("Starting training ")
for i in range(epochs):
cost = 0.0
for j in range(n_batches):
Xbatch = Xtrain[j*batchSize:(j+1)*batchSize]
Ybatch = Ytrain[j*batchSize:(j+1)*batchSize]
cost += train(model, loss, optimizer, Xbatch, Ybatch)
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
print('predicting...')
Ypred = predict(model, Xtest)
for k in range(0, len(Ytest)):
# print(y_hat[i])
l1 = np.abs(float(Ytest[k, 0]) - Ypred[k, 0])
l2 = np.abs(float(Ytest[k, 1]) - Ypred[k, 1])
l3 = np.abs(float(Ytest[k, 2]) - Ypred[k, 2])
l4 = np.abs(float(Ytest[k, 3]) - Ypred[k, 3])
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1, l1)
max_2 = max(max_2, l2)
max_3 = max(max_3, l3)
max_4 = max(max_4, l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
loss1 /= len(Ytest)
loss2 /= len(Ytest)
loss3 /= len(Ytest)
loss4 /= len(Ytest)
total_loss = loss1 + loss2 + loss3 + loss4
total_loss /= 4.0
print('median: %.3f %.3f %.3f %.3f' % (np.median(list_1), np.median(list_2), np.median(list_3), np.median(list_4)))
print('max loss: %.3f %.3f %.3f %.3f' % (max_1, max_2, max_3, max_4))
print('Real test score: %.3f %.3f %.3f %.3f' % (loss1, loss2, loss3, loss4))
print("Epoch: %d, acc: %.3f" % (i, total_loss))
costs.append(cost/n_batches)
test_accuracies.append(round(total_loss, 3))
torch.save(model.state_dict(), "LPC_NN.pt")
print(test_accuracies)
+114
View File
@@ -0,0 +1,114 @@
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
test_data = np.load("timitTest.npy")
Xtest = test_data[:,5:].astype(np.float32)
Ytest = test_data[:,1:5].astype(np.float32)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
_, D = Xtest.shape
print(D)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.Dense1 = nn.Linear(D, 1024)
self.Dense2 = nn.Linear(1024, 512)
self.Dense3 = nn.Linear(512, 256)
self.out = nn.Linear(256, 4)
def forward(self, x):
x = torch.sigmoid(self.Dense1(x))
x = torch.sigmoid(self.Dense2(x))
x = torch.sigmoid(self.Dense3(x))
return self.out(x)
def scaledLoss(output, target):
scale = torch.tensor([2.0, 1.0, .5, .1]).to(device)
loss = torch.abs(output - target)
scaled = loss*scale
return torch.mean(scaled)
#loss = nn.L1Loss()
def train(model, optimizer, inputs, labels):
inputs = Variable(inputs.to(device))
labels = Variable(labels.to(device))
optimizer.zero_grad()
logits = model.forward(inputs)
output = scaledLoss(logits, labels)
output.backward()
optimizer.step()
return output.item()
def predict(model, inputs):
inputs = Variable(inputs)
logits = model.forward(inputs.to(device))
return logits.data.cpu().numpy()
torch.manual_seed(0)
Xtest = torch.from_numpy(Xtest).float().to(device)
Ytest = torch.from_numpy(Ytest).float().to(device)
model = Net().to(device)
optimizer = optim.Adagrad(model.parameters(), lr=0.01)
model.load_state_dict(torch.load("LPC_NN_scaledLoss.pt"))
model.eval()
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
print('predicting...')
Ypred = predict(model, Xtest)
for k in range(0, len(Ytest)):
# print(y_hat[i])
l1 = np.abs(float(Ytest[k, 0]) - Ypred[k, 0])
l2 = np.abs(float(Ytest[k, 1]) - Ypred[k, 1])
l3 = np.abs(float(Ytest[k, 2]) - Ypred[k, 2])
l4 = np.abs(float(Ytest[k, 3]) - Ypred[k, 3])
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1, l1)
max_2 = max(max_2, l2)
max_3 = max(max_3, l3)
max_4 = max(max_4, l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
loss1 /= len(Ytest)
loss2 /= len(Ytest)
loss3 /= len(Ytest)
loss4 /= len(Ytest)
total_loss = loss1 + loss2 + loss3 + loss4
total_loss /= 4.0
print('median: %.3f %.3f %.3f %.3f' % (np.median(list_1), np.median(list_2), np.median(list_3), np.median(list_4)))
print('max loss: %.3f %.3f %.3f %.3f' % (max_1, max_2, max_3, max_4))
print('Real test score: %.3f %.3f %.3f %.3f' % (loss1, loss2, loss3, loss4))
print("acc: %.3f" % (total_loss))
BIN
View File
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
+2 -2
View File
@@ -39,8 +39,8 @@ cd ~/torch; bash install-deps;
```
luarocks install rnn
```
The Estimation model can be downloaded here and because of size constraints the Tracking model can be abtained by download from this link
[tracking_model.mat] (https://drive.google.com/open?id=0Bxkc5_D0JjpiZWx4eTU1d0hsVXc)
The Estimation model can be downloaded here and because of size constraints the Tracking model can be abtained by download from this link
[tracking_model.dat.gz](https://drive.google.com/open?id=1-BwlbbHykIV52c-SL1ofcppxZ5pTTXai)
## How to use:
+142
View File
@@ -0,0 +1,142 @@
from keras.models import model_from_json
import numpy as np
import csv
import math
model = model_from_json(open('model.json').read())
model.load_weights('weights.h5')
data_dir = ""
X_test = np.load(data_dir+'VTR_test_X.npy')
Y = np.load(data_dir+'VTR_test_Y.npy')
names = Y[:, :1]
Y_test = Y[:,1:]
predictions = []
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
male = [0.0, 0.0, 0.0, 0.0, 0.0, [], [], [], []]
female = [0.0, 0.0, 0.0, 0.0, 0.0, [], [], [], []]
karma_list = [0, 0.0, 0.0, 0.0, 0.0]
AVG_list = [0, 0.0, 0.0, 0.0, 0.0]
y_hat = model.predict(X_test)
for i in range(0,len(Y_test)):
l1 = np.abs(float(Y_test[i, 0]) - y_hat[i, 0])
l2 = np.abs(float(Y_test[i, 1]) - y_hat[i, 1])
l3 = np.abs(float(Y_test[i, 2]) - y_hat[i, 2])
l4 = np.abs(float(Y_test[i, 3]) - y_hat[i, 3])
pred = [names[i][0], float(Y_test[i, 0]), float(Y_test[i, 1]), float(Y_test[i, 2]), float(Y_test[i, 3])]
AVG_list[0] += 1
AVG_list[1] += float(Y_test[i, 0]) - y_hat[i, 0]
AVG_list[2] += float(Y_test[i, 1]) - y_hat[i, 1]
AVG_list[3] += float(Y_test[i, 2]) - y_hat[i, 2]
AVG_list[4] += float(Y_test[i, 3]) - y_hat[i, 3]
pred.extend([y_hat[i, 0], y_hat[i, 1], y_hat[i, 2], y_hat[i, 3]])
if names[i][0].split('_')[3][0] == 'f':
female[0] += 1
female[1] += l1
female[2] += l2
female[3] += l3
female[4] += l4
female[5].append(l1)
female[6].append(l2)
female[7].append(l3)
female[8].append(l4)
elif names[i][0].split('_')[3][0] == 'm':
male[0] += 1
male[1] += l1
male[2] += l2
male[3] += l3
male[4] += l4
male[5].append(l1)
male[6].append(l2)
male[7].append(l3)
male[8].append(l4)
predictions.append(pred)
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1,l1)
max_2 = max(max_2,l2)
max_3 = max(max_3,l3)
max_4 = max(max_4,l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
karma_list[0] += 1
karma_list[1] += l1 * l1
karma_list[2] += l2 * l2
karma_list[3] += l3 * l3
karma_list[4] += l4 * l4
loss1 /= len(Y_test)
loss2 /= len(Y_test)
loss3 /= len(Y_test)
loss4 /= len(Y_test)
total_loss = loss1+loss2+loss3+loss4
total_loss /= 4.0
print('standard deviation', round(np.std(list_1)*1000, 2), round(np.std(list_2)*1000, 2), round(np.std(list_3)*1000, 2), round(np.std(list_4)*1000, 2))
print('median', round(np.median(list_1)*1000, 2), round(np.median(list_2)*1000, 2), round(np.median(list_3)*1000, 2), round(np.median(list_4)*1000, 2))
print('max loss ', round(max_1*1000, 2), round(max_2*1000, 2), round(max_3*1000, 2), round(max_4*1000, 2))
print('total loss ', round(total_loss*1000, 2))
print('Real test score:', round(loss1*1000, 2), round(loss2*1000, 2), round(loss3*1000, 2), round(loss4*1000, 2))
female[1] = round((female[1] / female[0])*1000, 2)
female[2] = round((female[2] / female[0])*1000, 2)
female[3] = round((female[3] / female[0])*1000, 2)
female[4] = round((female[4] / female[0])*1000, 2)
female[5] = round(np.std(female[5])*1000, 2)
female[6] = round(np.std(female[6])*1000, 2)
female[7] = round(np.std(female[7])*1000, 2)
female[8] = round(np.std(female[8])*1000, 2)
male[1] = round((male[1] / male[0])*1000, 2)
male[2] = round((male[2] / male[0])*1000, 2)
male[3] = round((male[3] / male[0])*1000, 2)
male[4] = round((male[4] / male[0])*1000, 2)
male[5] = round(np.std(male[5])*1000, 2)
male[6] = round(np.std(male[6])*1000, 2)
male[7] = round(np.std(male[7])*1000, 2)
male[8] = round(np.std(male[8])*1000, 2)
print("male: ", male)
print("female: ", female)
# karma
karma_list[1] /= karma_list[0]
karma_list[2] /= karma_list[0]
karma_list[3] /= karma_list[0]
karma_list[4] /= karma_list[0]
print('root mean squared error ', round(math.sqrt(karma_list[1]) * 1000, 2), round(math.sqrt(karma_list[2]) * 1000, 2),
round(math.sqrt(karma_list[3]) * 1000, 2), round(math.sqrt(karma_list[4]) * 1000, 2))
AVG_list[1] /= AVG_list[0]
AVG_list[2] /= AVG_list[0]
AVG_list[3] /= AVG_list[0]
AVG_list[4] /= AVG_list[0]
print('AVG ', round(AVG_list[1] * 1000, 2), round(AVG_list[2] * 1000, 2), round(AVG_list[3] * 1000, 2), round(AVG_list[4] * 1000, 2))
with open("results/VTR.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(predictions)
+2 -1
View File
@@ -26,7 +26,8 @@ def build_data(wav,begin=None,end=None):
dstr = wav_in_file.readframes(N)
data = np.fromstring(dstr, np.int16)
if begin is not None and end is not None:
return data[begin*16000:end*16000]
#return data[begin*16000:end*16000] #numpy 1.11.0
return data[np.int(begin*16000):np.int(end*16000)] #numpy 1.14.0
X = []
l = len(data)
for i in range(0, l-100, 160):
+141
View File
@@ -0,0 +1,141 @@
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
torch.manual_seed(1)
trainY = np.load("norm_cnn_timit_train_Y.npy")
testY = np.load("norm_cnn_timit_test_Y.npy")
Xtrain = np.load("norm_cnn_timit_train_X.npy").astype(np.float32)
Ytrain = trainY[:,1:5].astype(np.float32)
Xtest = np.load("norm_cnn_timit_test_X.npy").astype(np.float32)
Ytest = testY[:,1:5].astype(np.float32)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
D = Xtrain.shape[1]
K = len(Ytrain)
print(D, K)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.Conv1 = nn.Conv2d(in_channels=1, out_channels=96, kernel_size=(3, 3), stride=1, padding=0)
self.Conv2 = nn.Conv2d(in_channels=96, out_channels=32, kernel_size=(3, 3), stride=1, padding=0)
self.Conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), stride=1, padding=0)
self.Conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(5, 5), stride=1, padding=0)
self.Dense5 = nn.Linear(43*38*64, 512)
self.out = nn.Linear(512, 4)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.Conv1(x))
x = F.relu(self.Conv2(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
x = F.relu(self.Conv3(x))
x = F.relu(self.Conv4(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
#print(in_size)
x = x.view(x.size(0), -1)
x = F.relu(self.Dense5(x))
return self.out(x)
def train(model, loss, optimizer, inputs, labels):
inputs = Variable(inputs.to(device))
labels = Variable(labels.to(device))
optimizer.zero_grad()
logits = model.forward(inputs)
output = loss.forward(logits, labels)
output.backward()
optimizer.step()
return output.item()
def predict(model, inputs):
inputs = Variable(inputs)
with torch.no_grad():
logits = model.forward(inputs.to(device))
return logits.data.cpu().numpy()
Xtrain = torch.from_numpy(Xtrain).float().to(device)
Ytrain = torch.from_numpy(Ytrain).float().to(device)
Xtest = torch.from_numpy(Xtest).float().to(device)
Ytest = torch.from_numpy(Ytest).float().to(device)
model = Net().to(device)
loss = nn.L1Loss()
optimizer = optim.Adagrad(model.parameters())
epochs = 80
batchSize = 32
n_batches = int(np.floor(Xtrain.size()[0]/batchSize))
costs = []
test_accuracies = []
print("Starting training ")
for i in range(epochs):
cost = 0.0
for j in range(n_batches):
#print(j, '/', n_batches)
Xbatch = Xtrain[j*batchSize:(j+1)*batchSize]
Ybatch = Ytrain[j*batchSize:(j+1)*batchSize]
cost += train(model, loss, optimizer, Xbatch, Ybatch)
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
print('predicting...')
Ypred = predict(model, Xtest)
for k in range(0, len(Ytest)):
# print(y_hat[i])
l1 = np.abs(float(Ytest[k, 0]) - Ypred[k, 0])
l2 = np.abs(float(Ytest[k, 1]) - Ypred[k, 1])
l3 = np.abs(float(Ytest[k, 2]) - Ypred[k, 2])
l4 = np.abs(float(Ytest[k, 3]) - Ypred[k, 3])
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1, l1)
max_2 = max(max_2, l2)
max_3 = max(max_3, l3)
max_4 = max(max_4, l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
loss1 /= len(Ytest)
loss2 /= len(Ytest)
loss3 /= len(Ytest)
loss4 /= len(Ytest)
total_loss = loss1 + loss2 + loss3 + loss4
total_loss /= 4.0
print('median: %.3f %.3f %.3f %.3f' % (np.median(list_1), np.median(list_2), np.median(list_3), np.median(list_4)))
print('max loss: %.3f %.3f %.3f %.3f' % (max_1, max_2, max_3, max_4))
print('Real test score: %.3f %.3f %.3f %.3f' % (loss1, loss2, loss3, loss4))
print("Epoch: %d, acc: %.3f" % (i, total_loss))
costs.append(cost/n_batches)
test_accuracies.append(round(total_loss, 3))
torch.save(model.state_dict(), "CNN_estimate.pt")
print(test_accuracies)
+121
View File
@@ -0,0 +1,121 @@
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
torch.manual_seed(1)
testY = np.load("norm_cnn_timit_test_Y.npy")
Xtest = np.load("norm_cnn_timit_test_X.npy").astype(np.float32)
Ytest = testY[:,1:5].astype(np.float32)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
D = Xtest.shape
print(D)
print(Xtest.shape[1], len(Ytest))
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.Conv1 = nn.Conv2d(in_channels=1, out_channels=96, kernel_size=(3, 3), stride=1, padding=0)
self.Conv2 = nn.Conv2d(in_channels=96, out_channels=32, kernel_size=(3, 3), stride=1, padding=0)
self.Conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), stride=1, padding=0)
self.Conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(5, 5), stride=1, padding=0)
self.Dense5 = nn.Linear(43*38*64, 512)
self.out = nn.Linear(512, 4)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.Conv1(x))
x = F.relu(self.Conv2(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
x = F.relu(self.Conv3(x))
x = F.relu(self.Conv4(x))
x = F.max_pool2d(x, kernel_size=2, stride=1)
#print(in_size)
x = x.view(x.size(0), -1)
x = F.relu(self.Dense5(x))
return self.out(x)
def train(model, loss, optimizer, inputs, labels):
inputs = Variable(inputs.to(device))
labels = Variable(labels.to(device))
optimizer.zero_grad()
logits = model.forward(inputs)
output = loss.forward(logits, labels)
output.backward()
optimizer.step()
return output.item()
def predict(model, inputs):
inputs = Variable(inputs)
with torch.no_grad():
logits = model.forward(inputs.to(device))
return logits.data.cpu().numpy()
Xtest = torch.from_numpy(Xtest).float().to(device)
Ytest = torch.from_numpy(Ytest).float().to(device)
model = Net().to(device)
loss = nn.L1Loss()
optimizer = optim.Adagrad(model.parameters())
model.load_state_dict(torch.load("CNN_estimate.pt"))
model.eval()
loss1 = 0.0
loss2 = 0.0
loss3 = 0.0
loss4 = 0.0
max_1 = 0.0
max_2 = 0.0
max_3 = 0.0
max_4 = 0.0
list_1 = []
list_2 = []
list_3 = []
list_4 = []
print('predicting...')
Ypred1 = predict(model, Xtest[:1000])
Ypred2 = predict(model, Xtest[1000:2000])
Ypred3 = predict(model, Xtest[2000:])
Ypred = np.concatenate((Ypred1, Ypred2, Ypred3))
for k in range(0, len(Ytest)):
# print(y_hat[i])
l1 = np.abs(float(Ytest[k, 0]) - Ypred[k, 0])
l2 = np.abs(float(Ytest[k, 1]) - Ypred[k, 1])
l3 = np.abs(float(Ytest[k, 2]) - Ypred[k, 2])
l4 = np.abs(float(Ytest[k, 3]) - Ypred[k, 3])
list_1.append(l1)
list_2.append(l2)
list_3.append(l3)
list_4.append(l4)
max_1 = max(max_1, l1)
max_2 = max(max_2, l2)
max_3 = max(max_3, l3)
max_4 = max(max_4, l4)
loss1 += l1
loss2 += l2
loss3 += l3
loss4 += l4
loss1 /= len(Ytest)
loss2 /= len(Ytest)
loss3 /= len(Ytest)
loss4 /= len(Ytest)
total_loss = loss1 + loss2 + loss3 + loss4
total_loss /= 4.0
print('median: %.3f %.3f %.3f %.3f' % (np.median(list_1), np.median(list_2), np.median(list_3), np.median(list_4)))
print('max loss: %.3f %.3f %.3f %.3f' % (max_1, max_2, max_3, max_4))
print('Real test score: %.3f %.3f %.3f %.3f' % (loss1, loss2, loss3, loss4))
print("acc: %.3f" % (total_loss))
-104
View File
@@ -1,104 +0,0 @@
require 'torch' -- torch
require 'optim'
require 'nn' -- provides a normalization operator
local train_file_path = 'train.th7'
local test_file_path = 'test.th7'
local train_data = torch.load(train_file_path)
local test_data = torch.load(test_file_path)
local Y = train_data[{{},{2,5}}]
local X = train_data[{{},{6,-1}}]
local test_labels = test_data[{{},{2,5}}]
local test_X = test_data[{{},{6,-1}}]
local batch_size = 30
epochs = 3
model = nn.Sequential() -- define the container
ninputs = 350; noutputs = 4 ; nhiddens1 = 1024; nhiddens2 = 512; nhiddens3 = 256
model:add(nn.Linear(ninputs,nhiddens1))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens1,nhiddens2))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens2,nhiddens3))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens3,noutputs))
criterion = nn.AbsCriterion()--MSECriterion()
x, dl_dx = model:getParameters()
sgd_params = {
learningRate = 0.01,
learningRateDecay = 1e-08,
weightDecay = 0,
momentum = 0
}
function train(X,Y)
current_loss = 0
for batch = 1,(#train_data)[1], batch_size do
local inputs = {}
local targets = {}
local x_start = batch
local x_end = math.min(batch + batch_size-1, (#train_data)[1])
for i = x_start,x_end do
local target = Y[i]
local input = X[i]
table.insert(inputs, input)
table.insert(targets, target)
end
local feval = function(x_new)
if x ~= x_new then
x:copy(x_new)
end
dl_dx:zero()
local f=0
for i = 1, #inputs do
local loss_x = criterion:forward(model:forward(inputs[i]), targets[i])
model:backward(inputs[i], criterion:backward(model.output, targets[i]))
f = f+loss_x
end
return f/#inputs, dl_dx:div(#inputs)
end
_,fs = optim.adagrad(feval,x,sgd_params)
current_loss = current_loss + fs[1]
end
current_loss = current_loss/( (#train_data)[1]/batch_size)
print('train loss = ' .. current_loss)
return current_loss
end
time = sys.clock()
local cumm_loss = 0.
for j = 1, epochs do
print(j)
cumm_loss = train( X, Y )
print( 'Final loss = ' .. cumm_loss )
if j%10 == 0 then
print('id approx text')
local loss1 = 0.0
local loss2 = 0.0
local loss3 = 0.0
local loss4 = 0.0
for i = 1,(#test_data)[1] do
local myPrediction = model:forward(test_X[i])
loss1 = loss1+math.abs(myPrediction[1] - test_labels[i][1])
loss2 = loss2+math.abs(myPrediction[2] - test_labels[i][2])
loss3 = loss3+math.abs(myPrediction[3] - test_labels[i][3])
loss4 = loss4+math.abs(myPrediction[4] - test_labels[i][4])
end
loss1 = loss1/(#test_data)[1]
loss2 = loss2/(#test_data)[1]
loss3 = loss3/(#test_data)[1]
loss4 = loss4/(#test_data)[1]
end
end
-- time taken
time = sys.clock() - time
print( "Time per epoch = " .. (time / epochs) .. '[s]')
print(loss1,loss2,loss3,loss4)
torch.save('estimation_model.dat',model)
-130
View File
@@ -1,130 +0,0 @@
require 'rnn'
require 'optim'
batchSize = 30
rho = 10
hiddenSize = 512
hiddenSize1 = 256
inputSize = 400
outputSize = 3
epochs = 100
xStart = 6
yStart = 2
yEnd = 4
local train_file_path = 'recurrent_train.th7'
local train_data = torch.load(train_file_path)
local Y = train_data[{{},{yStart,yEnd}}]
local X = train_data[{{},{xStart,-1}}]
seriesSize = (#train_data)[1]
print(seriesSize)
local test_file_path = 'recurrent_test.th7'
local test_data = torch.load(test_file_path)
local test_labels = test_data[{{},{yStart,yEnd}}]
local test_X = test_data[{{},{xStart,-1}}]
model = nn.Sequential()
model:add(nn.Sequencer(nn.FastLSTM(inputSize, hiddenSize, rho)))
model:add(nn.Sequencer(nn.FastLSTM(hiddenSize, hiddenSize1, rho)))
model:add(nn.Sequencer(nn.Linear(hiddenSize1, outputSize)))
criterion = nn.SequencerCriterion(nn.AbsCriterion())
-- dummy dataset (task predict the next item)
--dataset = torch.randn(seriesSize, inputSize)
-- define the index of the batch elements
offsets = {}
for i= 1, batchSize do
table.insert(offsets, i)--math.ceil(math.random() * batchSize))
end
offsets = torch.LongTensor(offsets)
function nextBatch()
local inputs, targets = {}, {}
for step = 1, rho do
--get a batch of inputs
table.insert(inputs, X:index(1, offsets))
-- shift of one batch indexes
offsets:add(1)
for j=1,batchSize do
if offsets[j] > seriesSize then
offsets[j] = 1
end
end
-- a batch of targets
table.insert(targets, Y[{{},{1,3}}]:index(1,offsets))
end
return inputs, targets
end
-- get weights and loss wrt weights from the model
x, dl_dx = model:getParameters()
feval = function(x_new)
-- copy the weight if are changed
if x ~= x_new then
x:copy(x_new)
end
-- select a training batch
local inputs, targets = nextBatch()
-- reset gradients (gradients are always accumulated, to accommodate
-- batch methods)
dl_dx:zero()
-- evaluate the loss function and its derivative wrt x, given mini batch
local prediction = model:forward(inputs)
local loss_x = criterion:forward(prediction, targets)
model:backward(inputs, criterion:backward(prediction, targets))
return loss_x, dl_dx
end
sgd_params = {
learningRate = 0.01,
learningRateDecay = 1e-08,
weightDecay = 0,
momentum = 0
}
time = sys.clock()
for j = 1, epochs do
-- train a mini_batch of batchSize in parallel
_, fs = optim.adagrad(feval,x, sgd_params)
print('error for iteration ' .. sgd_params.evalCounter .. ' is ' .. fs[1])
end
print('id approx text')
local loss1 = 0.0
local loss2 = 0.0
local loss3 = 0.0
local loss4 = 0.0
for i = 1,(#test_data)[1], 1 do
local inputs = {}
for step = 1, 1 do
--get a batch of inputs
table.insert(inputs, test_X[i])
end
local myPrediction = model:forward(inputs)
loss1 = loss1+math.abs(myPrediction[1][1] - test_labels[i][1])
loss2 = loss2+math.abs(myPrediction[1][2] - test_labels[i][2])
loss3 = loss3+math.abs(myPrediction[1][3] - test_labels[i][3])
--loss4 = loss4+math.abs(myPrediction[4] - test_labels[i][4])
end
loss1 = loss1/(#test_data)[1]
loss2 = loss2/(#test_data)[1]
loss3 = loss3/(#test_data)[1]
--loss4 = loss4/(#test_data)[1]
-- time taken
time = sys.clock() - time
print( "Time per epoch = " .. (time / epochs) .. '[s]')
print(loss1,loss2,loss3,loss4)
torch.save('recurrent.dat',model)
-129
View File
@@ -1,129 +0,0 @@
require 'torch' -- torch
require 'optim'
require 'nn' -- provides a normalization operator
local train_file_path = 'train.th7'
local test_file_path = 'test.th7'
local train_data = torch.load(train_file_path)
local test_data = torch.load(test_file_path)
local train_labels = train_data[{{},{2,5}}]
local train_X = train_data[{{},{6,-1}}]
local test_labels = test_data[{{},{2,5}}]
local test_X = test_data[{{},{6,-1}}]
local batch_size = 30
model = nn.Sequential() -- define the container
ninputs = 350; noutputs = 4 ; nhiddens1 = 1024; nhiddens2 = 512; nhiddens3 = 256
--model:add(nn.Linear(ninputs, noutputs)) -- define the only module
model:add(nn.Linear(ninputs,nhiddens1))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens1,nhiddens2))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens2,nhiddens3))
model:add(nn.Sigmoid())
model:add(nn.Linear(nhiddens3,noutputs))
criterion = nn.AbsCriterion()--MSECriterion()
x, dl_dx = model:getParameters()
feval = function(x_new)
if x ~= x_new then
x:copy(x_new)
end
-- select a new training sample
_nidx_ = (_nidx_ or 0) + 1
if _nidx_ > (#train_data)[1] then _nidx_ = 1 end
--local sample = data[_nidx_]
local target = train_labels[_nidx_] -- this funny looking syntax allows
local inputs = train_X[_nidx_] -- slicing of arrays.
-- reset gradients (gradients are always accumulated, to accommodate
-- batch methods)
dl_dx:zero()
-- evaluate the loss function and its derivative wrt x, for that sample
--print(inputs)
--print(target)
for i=1, 350 do
if type(inputs[i]) ~= 'number' then
print(i)
print(inputs[i])
print(type(inputs[i])) end
end
--io.write("continue with this operation (y/n)?")
--answer=io.read()
local loss_x = criterion:forward(model:forward(inputs), target)
model:backward(inputs, criterion:backward(model.output, target))
-- return loss(x) and dloss/dx
return loss_x, dl_dx
end
-- Given the function above, we can now easily train the model using SGD.
-- For that, we need to define four key parameters:
-- + a learning rate: the size of the step taken at each stochastic
-- estimate of the gradient
-- + a weight decay, to regularize the solution (L2 regularization)
-- + a momentum term, to average steps over time
-- + a learning rate decay, to let the algorithm converge more precisely
sgd_params = {
learningRate = 0.01,
learningRateDecay = 1e-08,
weightDecay = 0,
momentum = 0
}
-- We're now good to go... all we have left to do is run over the dataset
-- for a certain number of iterations, and perform a stochastic update
-- at each iteration. The number of iterations is found empirically here,
-- but should typically be determinined using cross-validation.
-- we cycle 1e4 times over our training data
for i = 1,1 do
print(i)
-- this variable is used to estimate the average loss
current_loss = 0
-- an epoch is a full loop over our training data
for i = 1,(#train_data)[1] do
-- optim contains several optimization algorithms.
-- All of these algorithms assume the same parameters:
-- + a closure that computes the loss, and its gradient wrt to x,
-- given a point x
-- + a point x
-- + some parameters, which are algorithm-specific
_,fs = optim.adagrad(feval,x,sgd_params)
-- Functions in optim all return two things:
-- + the new x, found by the optimization method (here SGD)
-- + the value of the loss functions at all points that were used by
-- the algorithm. SGD only estimates the function once, so
-- that list just contains one value.
current_loss = current_loss + fs[1]
end
-- report average error on epoch
current_loss = current_loss / (#train_data)[1]
print('train loss = ' .. current_loss)
end
----------------------------------------------------------------------
-- 5. Test the trained model.
-- Now that the model is trained, one can test it by evaluating it
-- on new samples.
-- The text solves the model exactly using matrix techniques and determines
-- that
-- corn = 31.98 + 0.65 * fertilizer + 1.11 * insecticides
-- We compare our approximate results with the text's results.
print('id approx text')
local loss1 = 0.0
local loss2 = 0.0
local loss3 = 0.0
local loss4 = 0.0
for i = 1,(#test_data)[1] do
local myPrediction = model:forward(test_X[i])
loss1 = loss1+math.abs(myPrediction[1] - test_labels[i][1])
loss2 = loss2+math.abs(myPrediction[2] - test_labels[i][2])
loss3 = loss3+math.abs(myPrediction[3] - test_labels[i][3])
loss4 = loss4+math.abs(myPrediction[4] - test_labels[i][4])
end
loss1 = loss1/(#test_data)[1]
loss2 = loss2/(#test_data)[1]
loss3 = loss3/(#test_data)[1]
loss4 = loss4/(#test_data)[1]
print(loss1,loss2,loss3,loss4)
torch.save('save.dat',model)
-109
View File
@@ -1,109 +0,0 @@
require 'rnn'
require 'optim'
function range(from, to, step)
step = step or 1
return function(_, lastvalue)
local nextvalue = lastvalue + step
if step > 0 and nextvalue <= to or step < 0 and nextvalue >= to or
step == 0
then
return nextvalue
end
end, nil, from - step
end
local train_file_path = 'recurrent_train.th7'
local test_file_path = 'recurrent_test.th7'
local train_data = torch.load(train_file_path)
local test_data = torch.load(test_file_path)
local Y = train_data[{{},{2,5}}]
local X = train_data[{{},{6,-1}}]
local test_labels = test_data[{{},{2,5}}]
local test_X = test_data[{{},{6,-1}}]
batchSize = 5
rho = 10
hiddenSize1 = 1024
hiddenSize2 = 512
hiddenSize3 = 256
inputSize = 1
outputSize = 1
seriesSize = 100
model = nn.Sequential()
model:add(nn.Sequencer(nn.FastLSTM(inputSize, hiddenSize2, rho)))
model:add(nn.Sequencer(nn.FastLSTM(hiddenSize2, hiddenSize3, rho)))
--model:add(nn.Sequencer(nn.Linear(hiddenSize2, hiddenSize3, rho)))
--model:add(nn.Sequencer(nn.Sigmoid()))
model:add(nn.Sequencer(nn.Linear(hiddenSize3, outputSize)))
criterion = nn.SequencerCriterion(nn.MSECriterion())
-- dummy dataset (task predict the next item)
--dataset = torch.randn(seriesSize, inputSize)
-- define the index of the batch elements
offsets = {}
for i= 1, batchSize do
table.insert(offsets,i)
end
offsets = torch.LongTensor(offsets)
print(offsets)
function nextBatch()
local inputs, targets = {}, {}
for step = 1, rho do
--get a batch of inputs
table.insert(inputs, X:index(1, offsets))
-- shift of one batch indexes
offsets:add(1)
for j=1,batchSize do
if offsets[j] > seriesSize then
offsets[j] = 1
end
end
-- a batch of targets
table.insert(targets, Y:index(1,offsets))
end
return inputs, targets
end
-- get weights and loss wrt weights from the model
x, dl_dx = model:getParameters()
feval = function(x_new)
-- copy the weight if are changed
if x ~= x_new then
x:copy(x_new)
end
-- select a training batch
local inputs, targets = nextBatch()
-- reset gradients (gradients are always accumulated, to accommodate
-- batch methods)
dl_dx:zero()
-- evaluate the loss function and its derivative wrt x, given mini batch
local prediction = model:forward(inputs)
local loss_x = criterion:forward(prediction, targets)
model:backward(inputs, criterion:backward(prediction, targets))
return loss_x, dl_dx
end
sgd_params = {
learningRate = 0.01,
learningRateDecay = 1e-08,
weightDecay = 0,
momentum = 0
}
for i = 1, 2 do
-- train a mini_batch of batchSize in parallel
_, fs = optim.adagrad(feval,x, sgd_params)
if sgd_params.evalCounter % 100 == 0 then
print('error for iteration ' .. sgd_params.evalCounter .. ' is ' .. fs[1] / rho)
end
end
-144
View File
@@ -1,144 +0,0 @@
require 'rnn'
require 'optim'
batchSize = 30
rho = 20
hiddenSize = 512
hiddenSize1 = 256
inputSize = 400
outputSize = 4
epochs = 10000
xStart = 6
yStart = 2
yEnd = 5
local train_file_path = 'recurrent_train.th7'
local train_data = torch.load(train_file_path)
local Y = train_data[{{},{yStart,yEnd}}]
local X = train_data[{{},{xStart,-1}}]
local place = train_data[{{},{1}}]
seriesSize = (#train_data)[1]
print(seriesSize)
local test_file_path = 'recurrent_test.th7'
local test_data = torch.load(test_file_path)
local test_labels = test_data[{{},{yStart,yEnd}}]
local test_X = test_data[{{},{xStart,-1}}]
model = nn.Sequential()
model:add(nn.Sequencer(nn.FastLSTM(inputSize, hiddenSize, rho)))
model:add(nn.Sequencer(nn.FastLSTM(hiddenSize, hiddenSize1, rho)))
model:add(nn.Sequencer(nn.Linear(hiddenSize1, outputSize)))
criterion = nn.SequencerCriterion(nn.AbsCriterion())
--local method = 'xavier'
--local model_new = require('weight-init')(model, method)
-- define the index of the batch elements
offsets = {}
function offset_(seed)
offsets = {}
math.randomseed(seed)
for i= 1, batchSize do
table.insert(offsets, math.ceil(math.random() * batchSize))
end
offsets = torch.LongTensor(offsets)
end
function nextBatch()
local inputs, targets = {}, {}
local nums = {}
for step = 1, rho do
--get a batch of inputs
table.insert(inputs, X:index(1, offsets))
-- shift of one batch indexes
offsets:add(1)
for j=1,batchSize do
if offsets[j] > seriesSize then
offsets[j] = 1
end
end
-- a batch of targets
table.insert(targets, Y[{{},{1,4}}]:index(1,offsets))
table.insert(nums,place:index(1,offsets))
end
return inputs, targets
end
-- get weights and loss wrt weights from the model
x, dl_dx = model:getParameters()
feval = function(x_new)
-- copy the weight if are changed
if x ~= x_new then
x:copy(x_new)
end
-- select a training batch
local inputs, targets = nextBatch()
-- reset gradients (gradients are always accumulated, to accommodate
-- batch methods)
dl_dx:zero()
-- evaluate the loss function and its derivative wrt x, given mini batch
local prediction = model:forward(inputs)
local loss_x = criterion:forward(prediction, targets)
model:backward(inputs, criterion:backward(prediction, targets))
return loss_x, dl_dx
end
adagrad_params = {
learningRate = 0.01,
learningRateDecay = 1e-08,
weightDecay = 0,
momentum = 0
}
seed = 1
offset_(seed)
time = sys.clock()
for j = 1, epochs do
if j%1000 == 0 then
seed = seed + 1
offset_(seed)
end
-- train a mini_batch of batchSize in parallel
_, fs = optim.adagrad(feval,x, adagrad_params)
print('error for iteration ' .. adagrad_params.evalCounter .. ' is ' .. fs[1]/rho)
end
print('id approx text')
local loss1 = 0.0
local loss2 = 0.0
local loss3 = 0.0
local loss4 = 0.0
predict_batch = 100
for i = 1,(#test_data)[1], predict_batch do
local inputs = {}
for step = 0, predict_batch-1 do
--get a batch of inputs
table.insert(inputs, test_X[i+step])
end
local myPrediction = model:forward(inputs)
for step = 1, predict_batch do
loss1 = loss1+math.abs(myPrediction[step][1] - test_labels[i+step-1][1])
loss2 = loss2+math.abs(myPrediction[step][2] - test_labels[i+step-1][2])
loss3 = loss3+math.abs(myPrediction[step][3] - test_labels[i+step-1][3])
loss4 = loss4+math.abs(myPrediction[4] - test_labels[i][4])
end
end
loss1 = loss1/(#test_data)[1]
loss2 = loss2/(#test_data)[1]
loss3 = loss3/(#test_data)[1]
loss4 = loss4/(#test_data)[1]
-- time taken
time = sys.clock() - time
print( "Time per epoch = " .. (time / epochs) .. '[s]')
print(loss1,loss2,loss3,loss4)
torch.save('recurrent3.dat',model)