我在这个领域是新手,也许这就是为什么我使事情变得混乱。我无法获得所需的结果,因为它显示的准确性非常低,这意味着我做错了。
import sklearn
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import matplotlib.pyplot as plt
import sklearn.model_selection
from tensorflow.keras import layers
data= pd.read_csv("u.csv")
#print(data.head())
plt.plot(data)
plt.show()
import tensorflow
x=data.iloc[:,:3].values
y=data.iloc[:,-1].values
n_features = 1
n_steps = 3
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x,y, test_size=0.1)
xtr=x_train
# create and fit the network
n_features = 1
x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], n_features))
model = Sequential()
# expected input data shape: (batch_size, timesteps, data_dim)
model = tensorflow.keras.Sequential()
model.add(layers.Embedding(input_dim=3, output_dim=1)) # returns a sequence of vectors of dimension
model.add(layers.SimpleRNN(2, return_sequences=True)) # returns a sequence of vectors of dimension
model.add(layers.SimpleRNN(2)) # return a single vector of dimension 32
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train)
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], n_features))
y_pred=model.predict(x_train)
#print(x_train.shape)
#print(y_train.shape)
print(model.layers[0].get_weights()[0]) # W - input weights
print(model.layers[0].get_weights()[1]) # U - recurrent weights
print(model.layers[0].get_weights()[2]) # b - output
m=model.layers[0].get_weights()[0]
答案 0 :(得分:0)
如果要使用pytorch创建NN。 NN的类型很多,三个组成部分是:
FFN:
让我们以FNN开头: FNN由三层组成:
对于MNIST而言,每个输入层的形状都会发生变化,并且每个输入层都有自己的行为
FNN的代码是:
( SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) , CITY
FETCH FIRST 1 ROW ONLY )
UNION ALL
( SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY
FETCH FIRST 1 ROW ONLY )
RNN的代码是:
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
train_dataset = dsets.MNIST(root="./data",
train=True,
transform=transforms.ToTensor(),
download=True
)
test_dataset = dsets.MNIST(root="./data",
train=False,
transform=transforms.ToTensor())
batch_size=100
n_iters = 3000
n_epochs = n_iters / (len(train_dataset)/batch_size)
n_epochs = int(n_epochs)
train_dataloader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_dataloader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
class FeedforwardNNModelSIG(nn.Module):
def __init__(self,input_dim,hidden_dim,output_dim):
super().__init__()
#Linear Layar
self.fc1=nn.Linear(input_dim,hidden_dim)
#Non Linear Layaer
self.sigmoid=nn.Sigmoid()
#Linear Layar(readout layer)
self.fc2=nn.Linear(hidden_dim,output_dim)
def forward(self,x):
# Linear
out = self.fc1(x)
# Non-Linear
out = self.sigmoid(out)
# Linear (readout layer)
out = self.fc2(out)
return out
input_dim=28*28
hidden_dim=50
output_dim=10
model = FeedforwardNNModelSIG(input_dim,hidden_dim,output_dim)
criterion = nn.CrossEntropyLoss()
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
iter=0
for epoch in range(n_epochs):
for i,(images, labels) in enumerate(train_dataloader):
##1 Convert inputs/labels to Variable
images = Variable(images.view(-1,28*28))
labels = Variable(labels)
##2 Clear the gradients
optimizer.zero_grad()
##3 Get output given input
outputs = model(images)
##4 Get Loss
loss = criterion(outputs,labels)
##5 Get gradients wrt parameters
loss.backward()
##6 Update paramters using Gradients
optimizer.step()
##7 Repeat
iter +=1
if iter % 500 == 0 :
correct = 0.
total = 0.
# Iter thorough test dataset
for images,labels in test_dataloader:
images = Variable(images.view(-1,28*28))
labels = Variable(labels)
outputs = model(images)
# Get the max value of the prediction
_,predicted=torch.max(outputs.data,1)
# number of labels
total += labels.size(0)
# Total correct predictions
correct += (predicted==labels).sum()
accuracy = 100*(correct/total)
print("Iteration: {} , Loss:{} , Accuracy:{}".format(iter,loss.data,accuracy))