Hi Zusammen,
da ich kein richtiges Forum finde und meine ersten Erfahrungen im Programmieren mit AutoIt gemacht habe und auch heute noch sehr viel benutze, entscheide ich mich einfach mal dazu dieses Forum zu nutzen
Vielleicht gibt es ja den ein oder anderen der auch schon mit neuronalen Netzwerken zu tun hatte und kann seine Meinung oder Verbesserungen abgeben.
Programmiert wird auf der PyCharm Community Edition mit anaconda3 auf Python 3.7 basis.
Ich habe mich mal dran versucht ein dynamisches Modell zu bauen dass nach bestmöglichen Modellen sucht. Man muss sagen, ich bin noch relativ unerfahren im Aufbau der neuronalen Netzwerken, habe jedoch schon durch mehrere Tutorials ein wenig wissen aufgebaut. Ob meine programmierung jedoch stimmt sei dahingestellt Es läuft, bringt aber nicht die gewünschten Ergebnissen.
Das neuronale Netz soll anhand von mehreren Inputs einen Output berechnen. Dieser Output ist eine Zahl zwischen 50 und ca. 250 und wird einfach durch 100 geteilt um dem neuronalen Netz es zu vereinfachen über die Neuronen und Bias etc. Somit haben wir als Output eine Zahl zwischen 0.05 und 2.5.
Der Code ist um gottes Willen nicht perfekt und sehr verbesserungswürdig. Aber genau deswegen bin ich hier.
Ich möchte gerne ein Konstrukt bauen, dass selbstständig nach dem besten Setup sucht und falls ein Setup "schlecht" ist es verworfen wird und ein neues beginnt. Activation Func in Output? Lieber Linear oder Non-Linear(mit activation)...
Beschreibung: Destillationsanlage mit Rückführung. Es soll eine bestimmte Menge Destillat(Output) produziert werden. Dabei spielt die Eingangsmenge "Feed", die Temperatur des Verdampfers und das wieder in die Anlage zurückgeführte Destillat "Reflux" eine große Rolle. Die Parameter A bis E geben jeweils die Zusammensetzung des Stoffes an. Parameter A, B und E destillieren z.B. bei einer geringeren Temperatur. C und D haben im Destillat nichts zu suchen. Durch die Rückführung wird z.B. der Anteil an A, B und E erhöht und somit der Ausgangsstoff "verdünnt" wodurch die Parameter C und D extrahiert werden. Destillationsanlage eben ;).
Verbesserungen und Ideen sind erwünscht! Die Dynamische Erstellung kann natürlich auch auf weitere andere Layer erweitert werden. Das Setup muss natürlich zu der Eingabe passen. Bildbearbeitung hat natürlich einen anderen Ansatz, falls man die Conv. Layer aber auch hier einbinden könnte, her damit. Danke
Beispielcode 5 Zeilen Input:
tensor([[3.5000000000e+02, 3.1950000000e+02, 2.4680000305e+02, 2.9270000458e+00,
2.7205999374e+01, 3.4679999352e+00, 2.6322999954e+01, 3.1165000916e+01],
[3.8000000000e+02, 3.1950000000e+02, 2.4700000000e+02, 2.7620000839e+00,
2.9021999359e+01, 3.6809999943e+00, 2.6806999207e+01, 2.8224000931e+01],
[2.2000000000e+02, 3.5300000000e+02, 2.4400000000e+02, 4.9959998131e+00,
2.8895000458e+01, 9.1900002956e-01, 6.2230000496e+00, 5.2880001068e+01],
[2.3000000000e+02, 2.6800000000e+02, 2.4750000000e+02, 9.3000002205e-02,
3.8362998962e+01, 5.2449998856e+00, 3.8039001465e+01, 1.6130000353e+00],
[3.8000000000e+02, 3.2550000000e+02, 2.4700000000e+02, 2.7620000839e+00,
2.9021999359e+01, 3.6809999943e+00, 2.6806999207e+01, 2.8224000931e+01]])
Beispielcode 5 Zeilen passender Output:
tensor([[1.1399999857],
[1.1549999714],
[1.1900000572],
[0.8399999738],
[1.1000000238]])
import torch
import torch.nn as nn
import random
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import pandas as pd
from visdom import Visdom
import os
import numpy as np
# Default Configuration
#random.seed(1)
PATH = 'state_dict_model.pt'
n_epochs = 999999999
batch_size = 18
use_gpu = False
torch.set_printoptions(precision=10)
num_hidden_layers = random.randint(1, 5)
optimizer_model = 0
sgd_momentum = random.randint(20, 70) / 100
learningrate_init = random.uniform(0, 1)
learningrate = learningrate_init
max_abort = 50
cur_abort = 0
last_loss = 999999999 # startindex
last_epoch = 0
allowed_abort_reset = 5
cur_abort_reset = 0
activation_funcs = ["ELU", "Hardshrink", "Hardsigmoid", "Hardtanh", "Hardswish",
"LeakyReLU", "LogSigmoid", "PReLU", "ReLU", "ReLU6", "RReLU",
"SELU", "CELU", "GELU", "Sigmoid", "SiLU", "Softplus", "Softshrink",
"Softsign", "Tanh", "Tanhshrink", "Softmin", 'Softmax', 'Softmax2d',
"LogSoftmax"
]
dropout_layers = ["Alphadropout", "Dropout"]
activation_funcs.remove("Softmax2d")
activation_funcs.remove("Softmin")
activation_funcs.remove("Softmax")
activation_funcs.remove("LogSoftmax")
#python -m visdom.server
global plotter
plotter = VisdomLinePlotter(env_name='Neural_Network_Distillationprocess')
# determine the supported device
def get_device():
if torch.cuda.is_available() and use_gpu:
device = torch.device('cuda:0')
else:
device = torch.device('cpu') # don't have GPU
return device
# convert a df to tensor to be used in pytorch
def df_to_tensor(df):
device = get_device()
return torch.from_numpy(df.values).float().to(device)
class VisdomLinePlotter(object):
"""Plots to Visdom"""
def __init__(self, env_name='main'):
self.viz = Visdom()
self.env = env_name
self.plots = {}
def plot(self, var_name, split_name, title_name, x, y):
if var_name not in self.plots:
self.plots[var_name] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=self.env, opts=dict(
legend=[split_name],
title=title_name,
xlabel='Epochs',
ylabel=var_name
))
else:
self.viz.line(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name, update = 'append')
class MeinNetz(nn.Module):
def __init__(self, input_num, hidden_num, output_num):
global current_model
super(MeinNetz, self).__init__()
#torch.save(MeinNetz.state_dict(self), PATH)
self.training = True
self.index = 1
self.configuration = {"last_out_features": output_num,
"next_out_features": "",
"min_hidden_neuron": 2,
"max_hidden_neuron": 10,
"chance_for_dropout": 20, # 20%
"chance_hidden_norm": 5 # 20%
}
self.configuration["next_out_feature"] = random.randint(self.configuration["min_hidden_neuron"], self.configuration["max_hidden_neuron"])
self.forwarding_struc = torch.nn.Sequential()
# Add normalized Input Layer # Add Activation
MeinNetz.add_layer(self, type="Linear", in_features=input_num, out_features=self.configuration["next_out_feature"], bias=True if random.randint(0,1) == 1 else False, norm=True)
MeinNetz.add_activation(self, func=random.choice(activation_funcs), inplace=True if random.randint(0,1) == 1 else False)
for i in range(1, hidden_num+1):
# Add normalized Hidden Layer # Add Activation
self.configuration["last_out_features"] = self.configuration["next_out_feature"]
self.configuration["next_out_feature"] = random.randint(self.configuration["min_hidden_neuron"], self.configuration["max_hidden_neuron"])
MeinNetz.add_layer(self, type="Linear", in_features=self.configuration["last_out_features"], out_features=self.configuration["next_out_feature"], bias=True if random.randint(0,1) == 1 else False, norm=True if random.randint(1,100) <= self.configuration["chance_hidden_norm"] else False)
MeinNetz.add_activation(self, func=random.choice(activation_funcs), inplace=True if random.randint(0,1) == 1 else False)
if random.randint(1,100) <= self.configuration["chance_for_dropout"]:
MeinNetz.add_dropout(self, func=random.choice(dropout_layers), p=random.randint(1, 5)/10)
# Add Output Layer
MeinNetz.add_layer(self, type="Linear", in_features=self.configuration["next_out_feature"], out_features=output_num, bias=True if random.randint(0,1) == 1 else False, norm=False)
# Add Activation
MeinNetz.add_activation(self, func=random.choice(activation_funcs), inplace=True if random.randint(0,1) == 1 else False)
for p in self.parameters():
p.data.fill_(random.randint(30, 60) / 100)
def add_layer(self, **kwargs):
if kwargs["norm"] == True:
self.forwarding_struc.add_module(str(self.index), nn.LayerNorm(kwargs["in_features"]))
self.index += 1
if kwargs["type"] == "Linear":
self.forwarding_struc.add_module(str(self.index), nn.Linear(kwargs["in_features"], kwargs["out_features"], kwargs["bias"]))
self.index += 1
def add_activation(self, **kwargs):
default_parameterlist = {'alpha': 1.0, 'inplace': True, 'lambd': 0.5, 'min_val': -1,
'max_val': 1, 'negative_slope': 1e-2, 'num_parameters': 1, 'init': 0.25,
'lower': 0.125, 'upper': 1/3, 'beta': 1, 'treshold': 20, 'dim': None}
for key in default_parameterlist:
if key not in kwargs:
kwargs[key] = default_parameterlist[key]
if kwargs["func"] in activation_funcs:
if kwargs["func"] == "ELU":
self.forwarding_struc.add_module(str(self.index), nn.ELU(kwargs["alpha"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "Hardshrink":
self.forwarding_struc.add_module(str(self.index), nn.Hardshrink(kwargs["lambd"]))
self.index += 1
elif kwargs["func"] == "Hardsigmoid":
self.forwarding_struc.add_module(str(self.index), nn.Hardsigmoid(kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "Hardtanh":
self.forwarding_struc.add_module(str(self.index), nn.Hardtanh(kwargs["min_val"], kwargs["max_val"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "Hardswish":
self.forwarding_struc.add_module(str(self.index), nn.Hardswish(kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "LeakyReLU":
self.forwarding_struc.add_module(str(self.index), nn.LeakyReLU(kwargs["negative_slope"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "LogSigmoid":
self.forwarding_struc.add_module(str(self.index), nn.LogSigmoid())
self.index += 1
elif kwargs["func"] == "PReLU":
self.forwarding_struc.add_module(str(self.index), nn.PReLU(kwargs["num_parameters"], kwargs["init"]))
self.index += 1
elif kwargs["func"] == "ReLU":
self.forwarding_struc.add_module(str(self.index), nn.ReLU(kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "ReLU6":
self.forwarding_struc.add_module(str(self.index), nn.ReLU6(kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "RReLU":
self.forwarding_struc.add_module(str(self.index), nn.RReLU(kwargs["lower"], kwargs["upper"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "SELU":
self.forwarding_struc.add_module(str(self.index), nn.SELU(kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "CELU":
self.forwarding_struc.add_module(str(self.index), nn.CELU(kwargs["alpha"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "GELU":
self.forwarding_struc.add_module(str(self.index), nn.GELU())
self.index += 1
elif kwargs["func"] == "Sigmoid":
self.forwarding_struc.add_module(str(self.index), nn.Sigmoid())
self.index += 1
elif kwargs["func"] == "SiLU":
self.forwarding_struc.add_module(str(self.index), nn.SiLU())
self.index += 1
elif kwargs["func"] == "Softplus":
self.forwarding_struc.add_module(str(self.index), nn.Softplus(kwargs["beta"], kwargs["treshold"]))
self.index += 1
elif kwargs["func"] == "Softshrink":
self.forwarding_struc.add_module(str(self.index), nn.Softshrink(kwargs["lambd"]))
self.index += 1
elif kwargs["func"] == "Softsign":
self.forwarding_struc.add_module(str(self.index), nn.Softsign())
self.index += 1
elif kwargs["func"] == "Tanh":
self.forwarding_struc.add_module(str(self.index), nn.Tanh())
self.index += 1
elif kwargs["func"] == "Tanhshrink":
self.forwarding_struc.add_module(str(self.index), nn.Tanhshrink())
self.index += 1
elif kwargs["func"] == "Softmin":
self.forwarding_struc.add_module(str(self.index), nn.Softmin(kwargs["dim"]))
self.index += 1
elif kwargs["func"] == "Softmax":
self.forwarding_struc.add_module(str(self.index), nn.Softmax(kwargs["dim"]))
self.index += 1
elif kwargs["func"] == "Softmax2d":
self.forwarding_struc.add_module(str(self.index), nn.Softmax2d())
self.index += 1
elif kwargs["func"] == "LogSoftmax":
self.forwarding_struc.add_module(str(self.index), nn.LogSoftmax(kwargs["dim"]))
self.index += 1
def add_dropout(self, **kwargs):
default_parameterlist = {'p': 0.5, 'inplace': False}
for key in default_parameterlist:
if key not in kwargs:
kwargs[key] = default_parameterlist[key]
if kwargs["func"] in dropout_layers:
if kwargs["func"] == "AlphaDropout":
self.forwarding_struc.add_module(str(self.index), nn.AlphaDropout(kwargs["p"], kwargs["inplace"]))
self.index += 1
elif kwargs["func"] == "Dropout":
self.forwarding_struc.add_module(str(self.index), nn.Dropout(kwargs["p"], kwargs["inplace"]))
self.index += 1
def forward(self, x):
return self.forwarding_struc(x)
useddevice = get_device()
if os.path.isfile(PATH) and False:
netz = torch.load(PATH)
else:
netz = MeinNetz(8, random.randint(1, 7), 1).to(useddevice)
if optimizer_model == 0:
optimizer = optim.SGD(netz.parameters(), lr=learningrate, momentum=sgd_momentum)
elif optimizer_model == 1:
optimizer = optim.ASGD(netz.parameters(), lr=learningrate)
elif optimizer_model == 2:
optimizer = optim.Adam(netz.parameters(), lr=learningrate)
elif optimizer_model == 3:
optimizer = optim.Adamax(netz.parameters(), lr=learningrate)
elif optimizer_model == 4:
optimizer = optim.Adagrad(netz.parameters(), lr=learningrate)
scheduler = lr_scheduler.StepLR(optimizer, step_size=.001, gamma=0.05)
readCSV_train = pd.read_csv('train.csv')
X_train = df_to_tensor(pd.DataFrame(readCSV_train, columns=['Feed', 'Reflux', 'TEvaporator', 'A', 'B','C','D','E']))
y_train = df_to_tensor(pd.DataFrame(readCSV_train, columns=['Distillate']))
for epoch in range(n_epochs):
permutation = torch.randperm(X_train.size()[0]).to(useddevice)
# Iterate X batch size
for i in range(0, X_train.size()[0], batch_size):
indices = permutation[i:i + batch_size]
batch_x, batch_y = X_train[indices], y_train[indices]
if (str(useddevice) == "cpu"):
input = Variable(torch.FloatTensor(batch_x), requires_grad=True)
else:
input = Variable(torch.cuda.FloatTensor(batch_x), requires_grad=True)
out = netz(input)
if (str(useddevice) == "cpu"):
target = Variable(torch.FloatTensor(batch_y), requires_grad=True)
else:
target = Variable(torch.cuda.FloatTensor(batch_y), requires_grad=True)
criterion = nn.MSELoss(None, None, 'sum')
loss = criterion(out, target)/batch_size
loss.backward()
optimizer.step()
netz.zero_grad()
scheduler.step()
last_epoch += 1
if last_loss < 5:
plotter.plot('loss', 'train', 'Class Loss', epoch, loss.item())
if epoch%300 == 0:
torch.save(netz, PATH)
print(target)
print(out)
print(epoch, last_loss, loss)
if loss.item() > 0 and loss.item() < last_loss:
#print(out[35:, :5][:, :9])
last_loss = loss.item()
cur_abort = 0
else:
cur_abort += 1
if cur_abort >= max_abort and cur_abort_reset <= allowed_abort_reset:
cur_abort = 0
cur_abort_reset += 1
last_loss = loss.item()
if (loss.item() > 0.09 and last_epoch%300 == 0) or loss.item() > 0.005 and cur_abort >= max_abort:
last_epoch = 0
cur_abort = 0
last_loss = 9999999
netz = MeinNetz(8, random.randint(3, 7), 1).to(useddevice)
if optimizer_model == 0:
optimizer = optim.SGD(netz.parameters(), lr=learningrate, momentum=sgd_momentum)
elif optimizer_model == 1:
optimizer = optim.ASGD(netz.parameters(), lr=learningrate)
elif optimizer_model == 2:
optimizer = optim.Adam(netz.parameters(), lr=learningrate)
elif optimizer_model == 3:
optimizer = optim.Adamax(netz.parameters(), lr=learningrate)
elif optimizer_model == 4:
optimizer = optim.Adagrad(netz.parameters(), lr=learningrate)
scheduler = lr_scheduler.StepLR(optimizer, step_size=.01, gamma=0.1)
Alles anzeigen