【发布时间】:2022-01-25 21:01:27
【问题描述】:
我尝试编写神经网络。我有一个下载的.csv file,其中包含某些加密货币股票的开始时间和收盘价;我希望创建一个预测模型。
import matplotlib.pyplot as plt
import numpy as np
class NeuralNetwork:
def __init__(self, width, activation_function):
self.width=width
# activation function = 'Sigmoid' , 'ReLu')
self.activation_function=activation_function
# number of hidden layers
self.depth=len(width)-2
# Weight matrix
self.W= np.array([np.random.randn(self.width[i+1],self.width[i]) for i in range(self.depth+1)], dtype=object)
# bais vector
self.b= np.array([np.random.randn(self.width[i+1]) for i in range(self.depth+1)], dtype=object)
#learning rate
self.alpha = 0.1
def sigma(self,x):
if self.activation_function == 'Sigmoid':
return 1/(1+np.exp(-x))
else:
if x.any()>=0:
return x
else:
return 0
def d_sigma(self,x):
if self.activation_function == 'Sigmoid':
return self.sigma(x)*self.sigma(1-x)
else:
if x>=0:
return 1
else:
return 0
## vectorize the function
def sigma_vec(self,X):
return np.array([self.sigma(x) for x in X],dtype=np.float128)
def d_sigma_vec(self,X):
return np.array([self.d_sigma(x) for x in X],dtype=np.float128)
def h(self,x):
a=np.copy(self.b)
z=np.copy(self.b)
z[0]=np.dot(self.W[0],x)+self.b[0]
for i in range(self.depth):
a[i]=self.sigma_vec(z[i])
z[i+1]=np.dot(self.W[i+1],a[i])+self.b[i+1]
return z[-1]
def plot(self,plt,X,col):
dx = np.linspace(X[0],X[len(X)-1], 100)
h_dx = np.array([ self.h([x]) for x in dx],dtype=np.float128)
plt.plot(dx,h_dx,color=col)
def plot0(self,plt,col):
dx = np.linspace(0,1, 100)
h_dx = np.array([ self.h([x]) for x in dx],dtype=np.float128)
plt.plot(dx,h_dx,color=col)
def gradient_of_loss_function(self,h,y):
return h-y
def gradient(self,x,y):
a=np.copy(self.b)
z=np.copy(self.b)
z[0]=np.dot(self.W[0],x)+self.b[0]
for i in range(self.depth):
a[i]=self.sigma_vec(z[i])
z[i+1]=np.dot(self.W[i+1],a[i])+self.b[i+1]
gradient_b=np.copy(self.b)
gradient_W=np.copy(self.W)
gradient_b[-1]=self.gradient_of_loss_function(z[-1],y)
for i in reversed(range(self.depth)):
gradient_b[i]=np.dot(np.transpose(self.W[i+1]),gradient_b[i+1])*self.d_sigma_vec(z[i])
gradient_W[i+1]=np.outer(gradient_b[i+1],a[i])
gradient_W[0]=np.outer(gradient_b[0],x)
return gradient_b,gradient_W;
def batch_gradient(self,X,Y):
batch_gradient_W=0
batch_gradient_b=0
for i in range(len(X)):
gradient_b,gradient_W = self.gradient(X[i],Y[i])
batch_gradient_W += gradient_W
batch_gradient_b += gradient_b
return batch_gradient_b,batch_gradient_W;
def train(self,X,Y,iterate):
for i in range(iterate):
alpha= self.alpha/len(X)
batch_gradient_b,batch_gradient_W =self.batch_gradient(X,Y)
self.W -= alpha*batch_gradient_W
self.b -= alpha*batch_gradient_b
with open(mergedcsv, newline='') as csvfile:
rows=csv.reader(csvfile)
for row in rows:
i=i+1
print(i)
UsePercent = 1
datanum=i
with open(mergedcsv, newline='') as csvfile:
i=1
rows=csv.reader(csvfile)
data = []
data_axis = []
putime = []
hide_data = []
hide_data_axis = []
hide_putime = []
time=0
for row in rows:
# if i <=4 and i>1:
if i<=2:
i=i+1
continue;
i=i+1
if(i>datanum*UsePercent):
try:
hide_data.append(round(float(row[5]),2))
time= float(row[1])
time = int(time/1000)
hide_data_axis.append(time)
hide_putime.append(i)
except ValueError:
print("",end="")
print(time)
continue
try:
data.append(round(float(row[5]),2))
time= float(row[1])
time = int(time/1000)
data_axis.append(time)
putime.append(i)
except ValueError:
print("",end="")
print(time)
创建模型需要哪些合适的变量?这是我尝试过的一个;由于 sigmoid 激活函数的指数幂公式,它会导致溢出错误。我将不胜感激有关合适的 X 轴输入、Y 轴输入和宽度的建议。
Y= (np.array(data, dtype=np.float128).reshape(-1,1))[:28] #data = array of closing prices
X= (np.array(data_axis, dtype=np.float128).reshape(-1,1))[:28] #data_axis is an array representing the time
width=[1,5,3,1]
nn=NeuralNetwork(width,'Sigmoid')
plt.scatter(X,Y,color='r')
nn.plot0(plt,'b')
nn.train(X,Y,10)
nn.plot0(plt,'g')
plt.show()
提前致谢。
【问题讨论】:
-
我怀疑这样的预测会奏效,因为改变的原因不在数据集中——而且可能永远不会。价格变动的原因可能在新闻中。
标签: python neural-network gradient-descent