【问题标题】:How to save weights of a neural network如何保存神经网络的权重
【发布时间】:2015-06-25 04:19:00
【问题描述】:

我在将经过训练的神经网络的权重保存在文本文件中时遇到了问题。 这是我的代码

def nNetwork(trainingData,filename):

    lamda = 1
    input_layer = 1200
    output_layer = 10
    hidden_layer = 25
    X=trainingData[0]
    y=trainingData[1]
    theta1 = randInitializeWeights(1200,25)
    theta2 = randInitializeWeights(25,10)
    m,n = np.shape(X)
    yk = recodeLabel(y,output_layer)
    theta = np.r_[theta1.T.flatten(), theta2.T.flatten()]

    X_bias = np.r_[np.ones((1,X.shape[0])), X.T]
    #conjugate gradient algo
    result = scipy.optimize.fmin_cg(computeCost,fprime=computeGradient,x0=theta,args=(input_layer,hidden_layer,output_layer,X,y,lamda,yk,X_bias),maxiter=100,disp=True,full_output=True )
    print result[1]  #min value
    theta1,theta2 = paramUnroll(result[0],input_layer,hidden_layer,output_layer)
    counter = 0
    for i in range(m):
        prediction = predict(X[i],theta1,theta2)
        actual = y[i]
        if(prediction == actual):
            counter+=1
    print  str(counter *100/m) + '% accuracy'

    data = {"Theta1":[theta1],
            "Theta2":[theta2]}
    op=open(filename,'w')
    json.dump(data,op)
    op.close()

def paramUnroll(params,input_layer,hidden_layer,labels):
    theta1_elems = (input_layer+1)*hidden_layer
    theta1_size = (input_layer+1,hidden_layer)
    theta2_size = (hidden_layer+1,labels)
    theta1 = params[:theta1_elems].T.reshape(theta1_size).T
    theta2 = params[theta1_elems:].T.reshape(theta2_size).T
    return theta1, theta2

我收到以下错误 raise TypeError(repr(o) + " is not JSON serializable")

请给出一个解决方案或任何其他方法来保存权重,以便我可以轻松地将它们加载到其他代码中。

【问题讨论】:

  • theta1 或 theta2 或两者都不是 JSON 可序列化的。它们是函数 paramUnroll 返回的对象。那么它们是什么类型的物体呢?
  • @PaulCornelius theta1 和 theta2 是 numpy 数组
  • 试试theta1.tolist()。只要记住在从文件加载写入列表后再次初始化numpy.array

标签: python numpy machine-learning neural-network


【解决方案1】:

将 numpy 数组保存为纯文本的最简单方法是执行 numpy.savetxt(并使用 numpy.loadtxt 加载它)。但是,如果您想使用 JSON 格式保存这两个文件,您可以使用 StringIO 实例编写文件:

with StringIO as theta1IO:
    numpy.savetxt(theta1IO, theta1)
    data = {"theta1": theta1IO.getvalue() }
    # write as JSON as usual

您也可以使用其他参数来做到这一点。

要检索数据,您可以这样做:

# read data from JSON
with StringIO as theta1IO:
    theta1IO.write(data['theta1'])
    theta1 = numpy.loadtxt(theta1IO)

【讨论】:

  • 我们可以使用 numpy 将两个数组作为字典保存在一个文本文件中吗?保存文本
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2015-12-14
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多