标签: deeplearning4j

Deeplearning4j xor示例

我正在尝试使用deeplearning4j训练xor网络,但我认为我并没有真正了解如何使用数据集.

我想创建一个带有两个输入,两个隐藏神经元和一个输出神经元的NN.

这是我有的:

package org.deeplearning4j.examples.xor;

import org.deeplearning4j.eval.Evaluation;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
import org.deeplearning4j.nn.conf.distribution.UniformDistribution;
import org.deeplearning4j.nn.conf.layers.GravesLSTM;
import org.deeplearning4j.nn.conf.layers.RnnOutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction;

public class XorExample {
    public static void main(String[] args) {

        INDArray input = Nd4j.zeros(4, 2);
        INDArray labels = Nd4j.zeros(4, 1);

        input.putScalar(new int[] { 0, 0 }, 0);
        input.putScalar(new int[] { 0, 1 }, 0);

        input.putScalar(new int[] { 1, 0 }, 1);
        input.putScalar(new int[] …
Run Code Online (Sandbox Code Playgroud)

deeplearning4j

5
推荐指数
2
解决办法
1382
查看次数

如何为nd4j和deeplearning4j设置scala sbt项目

我想使用scala在我自己的sbt项目中运行deeplearning4j示例中的LSTM代码.我的设置是Ubuntu 14.04,sbt 0.13,Oracle Java 8,nd4j 0.5.0版,scala 2.11.8.我的方法可以在我的git repo中找到.随意克隆它.在运行时,我收到以下警告和错误.我怎样才能解决这个问题?

scala sbt nd4j deeplearning4j

5
推荐指数
1
解决办法
1050
查看次数

Deeplearning4j:Iterations,Epochs和ScoreIterationListener

大家下午好.

我是Deepleaning4j库的新手,还有一些我还不清楚的东西."时代"的概念并不新鲜,因此很明显它代表了训练集的一个完整周期.我的第一个疑问与"迭代"的概念有关.什么是训练集的迭代?它是否对应于小批量训练实例或其他内容的分析?

在我的代码中,我设置了".iterations(1)"; 但是,当我运行我的代码时,我看到了很多:

... ScoreIterationListener - 在迭代XX得分是yy.yyyyyy"

所以,如果我设置".iterations(1)",为什么我继续看到XX的值大于1?"迭代"作为网络配置参数和"迭代"对ScoreIterationListener类意味着什么?

感谢大家对任何有用信息的回答或链接.

最好,毛罗.

java deeplearning4j

5
推荐指数
1
解决办法
1095
查看次数

是否有任何使用加权损失进行像素分割/分类任务的例子?

我正在对我的数据进行FCN32语义分段.我运行算法来微调我的数据(只有一个通道的灰度图像),直到80,000次迭代; 然而,损失和准确度是波动的,输出图像完全是黑色的.甚至,在80,000次迭代后损失如此之高.我认为分类器不能很好地训练我的数据.所以,我要从头开始训练.另一方面,我的数据有不平衡的类成员.背景像素多于其他四个类.一些研究人员建议使用加权损失.有谁有想法吗?我做得对吗?如何将此加权损失添加到train_val.prototxt?

如果您了解与加权损失培训相关的任何资源/示例,我将非常感激,请在此与我分享.

再次感谢

neural-network deep-learning caffe pycaffe deeplearning4j

5
推荐指数
1
解决办法
606
查看次数

keras:使用 get_weights 函数提取权重

我想提取 1d CNN 层的权重,并了解如何准确计算预测值。我无法使用get_weights()函数的权重重新生成预测值。

为了说明我的理解,这里有一个小数据集。

n_filter = 64
kernel_size = 10
len_timeseries = 123
n_feature = 3
X = np.random.random(sample_size*len_timeseries*n_feature).reshape(sample_size,len_timeseries,n_feature)
y = np.random.random(sample_size*(len_timeseries-kernel_size+1)*n_filter).reshape(sample_size,
                                                                                  (len_timeseries-kernel_size+1),
                                                                                  n_filter)
Run Code Online (Sandbox Code Playgroud)

现在,创建一个简单的一维 CNN 模型,如下所示:

model = Sequential()
model.add(Conv1D(n_filter,kernel_size,
                 input_shape=(len_timeseries,n_feature)))
model.compile(loss="mse",optimizer="adam")
Run Code Online (Sandbox Code Playgroud)

拟合模型并预测如下值X

model.fit(X,y,nb_epoch=1)
y_pred = model.predict(X)
Run Code Online (Sandbox Code Playgroud)

的维度y_pred(1000, 114, 64)应该的。

现在,我想重现y_pred[irow,0,ilayer]]使用存储在model.layer. 由于只有单层,len(model.layer)=1. 所以我从第一层和唯一层中提取权重为:

weight = model.layers[0].get_weights()
print(len(weight))
> 2 
weight0 = np.array(weight[0])
print(weight0.shape)
> (10, 1, 3, 64)
weight1 = np.array(weight[1])
print(weight1.shape)
> (64,) …
Run Code Online (Sandbox Code Playgroud)

python keras deeplearning4j keras-layer

5
推荐指数
1
解决办法
8768
查看次数

在 ND4J/DL4J 中重复使用标准化器

我想知道在 ND4J/DL4J 中重用标准化器的正确方法是什么。目前,我将其保存如下:

final DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit( trainingData );
normalizer.transform( trainingData );
normalizer.transform( testData );

try {
    final NormalizerSerializer normalizerSerializer = new NormalizerSerializer();
    normalizerSerializer.addStrategy( new StandardizeSerializerStrategy() );
    normalizerSerializer.write( normalizer, path );
} catch ( final IOException e ) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

并通过以下方式加载:

try {
    final NormalizerSerializer normalizerSerializer = new NormalizerSerializer();
    normalizerSerializer.addStrategy( new StandardizeSerializerStrategy() );
    final DataNormalization normalizer = normalizerSerializer.restore( path );
} catch ( final Exception e ) { // Throws Exception instead of IOException.
    // …
Run Code Online (Sandbox Code Playgroud)

java nd4j deeplearning4j

5
推荐指数
1
解决办法
465
查看次数

将 deeplearning4j 模型导出到 Keras

我知道 deeplearning4j 可以从 Keras ( https://deeplearning4j.org/model-import-keras )导入模型,但我对相反的方式感兴趣。所以,

  • 有没有办法将 deeplearning4j 模型导出到 Keras?

这可以直接或通过某种方式转换使用 ModelSerializer https://deeplearning4j.org/modelpersistence存储的模型)?特别是,我对在 keras.js ( https://github.com/transcranial/keras-js )中使用训练有素的模型很感兴趣。

keras deeplearning4j

5
推荐指数
0
解决办法
435
查看次数

Rnn神经网络预测返回意外预测

我正在试图配置RNN神经网络,以预测5种不同类型的文本实体.我正在使用下一个配置:

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(seed)
            .iterations(100)
            .updater(Updater.ADAM)  //To configure: .updater(Adam.builder().beta1(0.9).beta2(0.999).build())
            .regularization(true).l2(1e-5)
            .weightInit(WeightInit.XAVIER)
            .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue).gradientNormalizationThreshold(1.0)
            .learningRate(2e-2)
            .trainingWorkspaceMode(WorkspaceMode.SEPARATE).inferenceWorkspaceMode(WorkspaceMode.SEPARATE)   //https://deeplearning4j.org/workspaces
            .list()
            .layer(0, new GravesLSTM.Builder().nIn(500).nOut(3)
                    .activation(Activation.TANH).build())
            .layer(1, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX)        //MCXENT + softmax for classification
                    .nIn(3).nOut(5).build())
            .pretrain(false).backprop(true).build();
  MultiLayerNetwork net = new MultiLayerNetwork(conf);
  net.init();
Run Code Online (Sandbox Code Playgroud)

我训练它然后我评估它.有用.不过我用的时候:

 int[] prediction = net.predict(features);
Run Code Online (Sandbox Code Playgroud)

有时它会回归并出现意想不到的预测.它返回正确的预测为1,2 .... 5但有时它返回数字为9,14,12 ...这个数字不对应于已识别的预测/标签.

为什么此配置会返回意外输出?

java deep-learning deeplearning4j rnn

5
推荐指数
1
解决办法
228
查看次数

在 keras 中加载模型后的不同预测

我有一个在 Keras 中构建的序列模型,经过训练后它给了我很好的预测,但是当我保存然后加载模型时,我没有在同一数据集上获得相同的预测。为什么?请注意,我检查了模型的权重,它们与模型的架构相同,使用 model.summary() 和 model.getWeights() 检查。这在我看来很奇怪,我不知道如何处理这个问题。我没有任何错误,但预测不同

  1. 我尝试使用 model.save() 和 load_model()

  2. 我尝试使用 model.save_weights() 然后重建模型然后加载模型

我对这两个选项都有同样的问题。

def Classifier(input_shape, word_to_vec_map, word_to_index, emb_dim, num_activation):

    sentence_indices = Input(shape=input_shape, dtype=np.int32)
    emb_dim = 300  # embedding di 300 parole in italiano
    embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index, emb_dim)

    embeddings = embedding_layer(sentence_indices)   

    X = LSTM(256, return_sequences=True)(embeddings)
    X = Dropout(0.15)(X)
    X = LSTM(128)(X)
    X = Dropout(0.15)(X)
    X = Dense(num_activation, activation='softmax')(X)

    model = Model(sentence_indices, X)

    sequentialModel = Sequential(model.layers)    
    return sequentialModel

    model = Classifier((maxLen,), word_to_vec_map, word_to_index, maxLen, num_activation)
    ...
    model.fit(Y_train_indices, Z_train_oh, epochs=30, …
Run Code Online (Sandbox Code Playgroud)

python neural-network keras tensorflow deeplearning4j

5
推荐指数
1
解决办法
2683
查看次数

如何从 ND4j 中的 NDArray 中选择给定的一组索引,类似于 numpy 的 arraydata[arrayIndex]?

我正在使用 ND4j(目前版本 1.0.0-beta5)开发一个严重依赖 Java 数组操作的科学应用程序。在整个管道中,我需要动态选择 [2,195102] 矩阵的不连续子集(更精确地说是几十/数百列)。知道如何在这个框架中实现这一目标吗?

简而言之,我正在尝试实现这个 python/numpy 操作:

import numpy as np
arrayData = np.array([[1, 5, 0, 6, 2, 0, 9, 0, 5, 2],
       [3, 6, 1, 0, 4, 3, 1, 4, 8, 1]])
arrayIndex = np.array((1,5,6))
res  = arrayData[:, arrayIndex]
# res value is
# array([[5, 0, 9],
#        [6, 3, 1]])
Run Code Online (Sandbox Code Playgroud)

到目前为止,我设法使用NDArray.getColumns函数(以及 indexArray 中的 NDArray.data().asInt() 来提供索引值)选择所需的列。问题在于,文档明确指出,关于计算过程中的信息检索,“请注意,这不应该用于速度”(请参阅​​ NDArray.ToIntMatrix()的文档以查看完整消息 - 不同的方法,相同的操作)。

我查看了NDArray.get()的不同原型,但似乎没有一个符合要求。我认为NDArray.getWhere()可能会起作用 - 如果它像我假设的那样只返回满足条件的元素 - 但到目前为止,使用它并不成功。在解释所需的参数/用法时,文档相对较少。

感谢大家的时间和帮助:)

编辑(04/11/2019):关于我所尝试的一些精确性。我尝试了 NDArray.get() 并使用了索引: …

java arrays nd4j deeplearning4j

5
推荐指数
1
解决办法
735
查看次数