org.nd4j.autodiff.samediff.config.OutputConfig Maven / Gradle / Ivy
/*
* Copyright (c) 2015-2019 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.nd4j.autodiff.samediff.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.nd4j.autodiff.listeners.Listener;
import org.nd4j.autodiff.samediff.SDVariable;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.autodiff.util.TrainingUtils;
import org.nd4j.base.Preconditions;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.adapter.MultiDataSetIteratorAdapter;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.dataset.api.iterator.MultiDataSetIterator;
/**
* Configuration for a {@link SameDiff} inference operation.
*
* Used in {@link SameDiff#output()}.
*/
@Getter
@Setter
public class OutputConfig {
@Setter(AccessLevel.NONE)
private SameDiff sd;
@NonNull
private List outputs = new ArrayList<>();
@NonNull
private List listeners = new ArrayList<>();
private MultiDataSetIterator data;
public OutputConfig(@NonNull SameDiff sd) {
this.sd = sd;
}
/**
* Add required outputs
*/
public OutputConfig output(@NonNull String... outputs) {
this.outputs.addAll(Arrays.asList(outputs));
return this;
}
/**
* Add required outputs
*/
public OutputConfig output(@NonNull SDVariable... outputs) {
String[] outNames = new String[outputs.length];
for (int i = 0; i < outputs.length; i++) {
outNames[i] = outputs[i].getVarName();
}
return output(outNames);
}
/**
* Set the data to use as input.
*/
public OutputConfig data(@NonNull MultiDataSetIterator data) {
this.data = data;
return this;
}
/**
* Set the data to use as input.
*/
public OutputConfig data(@NonNull DataSetIterator data) {
this.data = new MultiDataSetIteratorAdapter(data);
return this;
}
/**
* Add listeners for this operation
*/
public OutputConfig listeners(@NonNull Listener... listeners) {
this.listeners.addAll(Arrays.asList(listeners));
return this;
}
private void validateConfig() {
Preconditions.checkNotNull(data, "Must specify data. It may not be null.");
}
/**
* Do inference and return the results.
*
* Uses concatenation on the outputs of {@link #execBatches()} which may cause issues with some inputs. RNNs with
* variable time series length and CNNs with variable image sizes will most likely have issues.
*/
public Map exec() {
return sd.output(data, listeners, outputs.toArray(new String[0]));
}
/**
* Do inference and return the results in batches.
*/
public List