我已经训练了Python中的单层神经网络模型(一个没有keras和tensorflow的简单模型)。Cani在训练后如何保存它,以及python的重量,以及稍后如何加载?
所有可训练的参数(例如权重和偏见)都可以视为python列表或numpy阵列(这是最优选的)。
for Python列表:
如果您的可训练参数为Python列表,则可以使用pickle
。您可以像这样pickle
:
import pickle
# weights is a Python array
pickle.dump( weights , open( 'weights.pkl' , 'wb' ) )
您可以将几个对象组合在一起set
或list
和泡菜,以便您有一个文件。为了阅读,
weights = pickle.load( open( 'weights.pkl' , 'rb' ))
对于numpy数组:
使所有代码变得容易。使用np.array.save()
方法可以保存一个数组。
np.save( 'weights.npy' , weights )
并加载它,
weights = np.load( 'weights.npy' )
除了这些普遍的方法之外,例如将权重和偏差写入文本文件或CSV文件也可能起作用。另外,JSON文件可能会有所帮助。
,因此您自己写下来。您需要一些简单的步骤:
- 在您的神经网络代码中,将权重存储在变量中。可以简单地使用self.pure。重量是numpy ndarrays。例如,如果重量是在用10个神经元分层的10个神经元之间的层之间,则是10 * 100(或100 * 10)nd阵列。
- 使用
numpy.save
保存ndarray。 - 要下次使用网络,请使用
numpy.load
加载权重 - 在您的网络的第一个初始化中,请使用已加载的权重。别忘了,如果您的网络经过训练,则应将重量冻结。可以通过零学习率来完成。
嗨,我如何将我的权重存储在文件中以供以后使用python。
...
class Neural_Network:
def __init__(self, inputnodes, hiddennodes, outputnodes, learninggrate):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
print("Input Nodes:", self.inodes, "Hidden Nodes", self.hnodes, "Output Nodes", self.onodes)
#wih means weight input hidden layer
self.wih = (np.random.rand(self.hnodes, self.inodes) - 0.5)
#who means weight hidden output layer
self.who = (np.random.rand(self.onodes, self.hnodes) - 0.5)
#Statistically this one is better
#self.wih = np.random.normal(0.0, pow(self.inodes, -0.5),(self.hnodes, self.inodes))
#self.who = np.random.normal(0.0, pow(self.hnodes, -0.5),(self.onodes, self.hnodes))
print("Matrix 1: n",self.wih)
print("Matrix 2: n", self.who)
self.lr = learninggrate
#activiation function
self.activation_function = lambda x: scipy.special.expit(x)
pass
def train(self, inputs_list, targets_list):
#convert inputs list into 2D array
inputs = np.array(inputs_list, ndmin=2).T
targets = np.array(targets_list, ndmin=2).T
#calculate signals into hidden layer
hidden_inputs = np.dot(self.wih, inputs)
#calcualte signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
final_inputs = np.dot(self.who ,hidden_outputs)
# calculate the signals emerging from final output layer
final_outputs = self.activation_function(final_inputs)
#output layer error is the (target - actual)
output_errors = targets - final_outputs
#hidden layer error is the output_error, slit by weight recombined at hidden node
hidden_errors = np.dot(self.who.T, output_errors)
# update the weights for the links between the hidden and output layers
self.who += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden_outputs))
# update the weights for the links between the input and hidden layer
self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))
pass
def query(self, inputs_list):
# convert inputs list to 3D array
inputs = np.array(inputs_list, ndmin=2).T
#calcualte signals
hidden_inputs = np.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = np.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
...