在新数据上使用Keras模型

时间:2017-08-17 15:45:34

标签: python keras conv-neural-network

我在Keras成功培训了一个ConvNet,现在我想将它应用于新数据。

当然,我可以使用model.save和load_model,但是因为这会为每个分类加载我的整个模型,所以需要很长时间。有更优雅的方式吗?

我尝试通过保存权重和偏差并将其应用于我的数据以及我使用的激活功能来“重建”我的模型。但是,我没有像使用load_model那样获得相同的结果,所以我一定是犯了错误......

这种重建方法是否正确,还是有更优雅的方式?

编辑:我添加了我的代码,希望这是可以理解的......

# sl: List containing for each layer: number of parameter matrices (=2 for conv or fully 
# connected layer, =0 for flatten layer) & shapes of the matrices
# weight_list_lc: List containing my weight matrices
# biases_list_lc: List containing my biases
def Is_Trip_ConvNet(inp):
    n_layers = len(sl)
    curr_dim = inp.shape
    for l in range(1,n_layers):
        if sl[l][0] > 0: # weights available
            weights = np.array(weight_list_lc[l-1])
            weights = weights.reshape(np.array(sl[l][1])) # correct shape 
            biases = bias_list_lc[l-1]
            if len(sl[l][1]) == 3: # ConvLayer
                field_size = sl[l][1][0]
                n_nodes = int(curr_dim[0]-field_size+1)
                n_filt = sl[l][1][2]
                out = np.zeros((n_nodes,n_filt))
                for n in range(0,n_nodes):
                    for f in range(0,n_filt):
                        out[n,f] = np.sum(np.multiply(inp[n:n+field_size,:],weights[:,:,f]))
                    out[n,:] = out[n,:]+biases
            else: # Fully Connected Layer
                out = inp.dot(weights)
                out = out + biases
            if l == n_layers-1: # Output Unit
                out = 1/(1+np.exp(-out)) # Sigmoid
            else:
                out = out*(out>0) # ReLU
        else: # no weights available --> Flatten Layer
            out = inp.reshape(-1)
        inp = out
        curr_dim = inp.shape
    return (out > 0.5)

我的输入形状为(18 x 8)(18个时间步长,8个功能)。 我的网络目前由卷积层(感知字段大小= 5,内核= 6),展平层,具有10个节点和1个输出节点的完全连接层组成。所有激活函数都是整流的,除了输出一个,这是sigmoid。

我仔细检查了我的清单的索引,它们是正确的。抱歉,我无法上传所有数据集。如果你知道什么可能遗漏,我很高兴听到它!

0 个答案:

没有答案