home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (DuplicateToTimeSeriesVertex.java)

This example Java source code file (DuplicateToTimeSeriesVertex.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

data, duplicatetotimeseriesvertex, equalsandhashcode, graphvertex, indarray, invalidinputtypeexception, override, string

The DuplicateToTimeSeriesVertex.java Java example source code

/*
 *
 *  * Copyright 2016 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package org.deeplearning4j.nn.conf.graph.rnn;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.deeplearning4j.nn.conf.graph.GraphVertex;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.inputs.InvalidInputTypeException;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.nd4j.linalg.api.ndarray.INDArray;

/**DuplicateToTimeSeriesVertex is a vertex that goes from 2d activations to a 3d time series activations, by means of
 * duplication. That is, given a 2d input with shape [numExamples,nIn] duplicate each row to give output of
 * [numExamples,nIn,timeSeriesLength], where the activations are the same for all time steps.<br>
 * This method is used for example in sequence to sequence models.<br>
 * <b>Note: The length of the output time series (number of time steps) is determined by means of referencing one of the
 * inputs in the ComputationGraph. That is: Because the length of the time series may differ at runtime, we generally want the number
 * of time steps to match some other input; here, we are specifying the length of the output time series to be the same as
 * one of the input time series<br>
 * @author Alex Black
 */
@Data @EqualsAndHashCode(callSuper=false)
public class DuplicateToTimeSeriesVertex extends GraphVertex {

    private String inputName;

    /**
     * @param inputName Name of the input in the ComputationGraph network to use, to determine how long the output time
     *                  series should be. This input should (a) exist, and (b) be a time series input
     */
    public DuplicateToTimeSeriesVertex(@JsonProperty("inputName") String inputName){
        this.inputName = inputName;
    }

    @Override
    public GraphVertex clone() {
        return new DuplicateToTimeSeriesVertex(inputName);
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof DuplicateToTimeSeriesVertex)) return false;
        DuplicateToTimeSeriesVertex d = (DuplicateToTimeSeriesVertex)o;
        if(inputName == null && d.inputName != null || inputName != null && d.inputName == null) return false;
        return inputName == null || inputName.equals(d.inputName);
    }

    @Override
    public int hashCode() {
        return 534806565 ^ (inputName != null ? inputName.hashCode() : 0);
    }

    @Override
    public int numParams(boolean backprop){
        return 0;
    }

    @Override
    public org.deeplearning4j.nn.graph.vertex.GraphVertex instantiate(ComputationGraph graph, String name, int idx,
                                                                      INDArray paramsView, boolean initializeParams) {
        return new org.deeplearning4j.nn.graph.vertex.impl.rnn.DuplicateToTimeSeriesVertex(graph,name,idx,inputName);
    }

    @Override
    public InputType getOutputType(InputType... vertexInputs) throws InvalidInputTypeException {
        if(vertexInputs.length != 1) throw new InvalidInputTypeException("Invalid input type: cannot duplicate more than 1 input");
        if(vertexInputs[0].getType() != InputType.Type.FF){
            throw new InvalidInputTypeException("Invalid input type: cannot duplicate to time series non feed forward input (got: " + vertexInputs[0] + ")");
        }

        return InputType.recurrent(((InputType.InputTypeFeedForward)vertexInputs[0]).getSize());
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java DuplicateToTimeSeriesVertex.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.