All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nd4j.linalg.api.ops.impl.shape.Reshape Maven / Gradle / Ivy

The newest version!
/*
 *  ******************************************************************************
 *  *
 *  *
 *  * 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.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * 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.linalg.api.ops.impl.shape;

import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import onnx.Onnx;
import org.nd4j.autodiff.samediff.SDVariable;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.imports.descriptors.properties.PropertyMapping;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.tensorflow.framework.AttrValue;
import org.tensorflow.framework.GraphDef;
import org.tensorflow.framework.NodeDef;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@NoArgsConstructor
public class Reshape extends DynamicCustomOp {

    private long[] shape;

    public final static int C_ORDER = -99;
    public final static int F_ORDER = -127;

    public Reshape(SameDiff sameDiff, SDVariable i_v, long[] shape) {
        super(null, sameDiff, new SDVariable[]{i_v});
        this.shape = shape;
        //c ordering: see (char) 99 for c ordering and (char) 'f' is 102
        //note it has to be negative for the long array case only
        //to flag the difference between an ordering being specified
        //and a dimension.
        if(iArguments.isEmpty())
            addIArgument(C_ORDER);
        addIArgument(shape);
    }

    public Reshape(SameDiff sameDiff, SDVariable i_v, SDVariable shape) {
        super(null, sameDiff, new SDVariable[]{i_v, shape});
        if(iArguments.isEmpty())
            addIArgument(C_ORDER);
    }

    public Reshape(INDArray in, long... shape) {
        super(new INDArray[]{in}, null);
        this.shape = shape;
        //c ordering: see (char) 99 for c ordering and (char) 'f' is 102
        //note it has to be negative for the long array case only
        //to flag the difference between an ordering being specified
        //and a dimension.
        if(iArguments.isEmpty())
            addIArgument(C_ORDER);
        addIArgument(shape);
    }


    public Reshape(@NonNull INDArray in, @NonNull INDArray shape, INDArray out) {
        super(null, new INDArray[]{in, shape}, wrapOrNull(out), null, (List)null);
        if(iArguments.isEmpty())
            addIArgument(C_ORDER);
    }

    public Reshape(INDArray in, INDArray shape) {
        this(in, shape, null);
    }


    @Override
    public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map attributesForNode, GraphDef graph) {
        if (!nodeDef.containsAttr("TShape") && nodeDef.getInputCount() == 1) {
            this.shape = new long[]{};
            return;
        } else if(nodeDef.getInputCount() == 1){
            val shape = nodeDef.getAttrOrThrow("Tshape");
            if (!shape.hasShape()) {
                val shapeRet = new long[2];
                shapeRet[0] = 1;
                shapeRet[1] = shape.getValueCase().getNumber();
                this.shape = shapeRet;
            } else {
                val shapeVals = shape.getShape().getDimList();
                if (shapeVals.size() > 1) {
                    this.shape = new long[shapeVals.size()];
                    for (int i = 0; i < shapeVals.size(); i++) {
                        this.shape[i] = (int) shapeVals.get(i).getSize();
                    }
                } else {
                    this.shape = new long[2];
                    this.shape[0] = 1;
                    this.shape[1] = (int) shapeVals.get(0).getSize();
                }

            }

            //all TF is c

            if (this.shape != null) {
                addIArgument(this.shape);
            }
        }
    }

    @Override
    public void initFromOnnx(Onnx.NodeProto node, SameDiff initWith, Map attributesForNode, Onnx.GraphProto graph) {

    }


    @Override
    public Map> mappingsForFunction() {
        Map> ret = new HashMap<>();
        Map map = new HashMap<>();

        val shapeMapping = PropertyMapping.builder()
                .onnxAttrName("shape")
                .tfInputPosition(-1)
                .propertyNames(new String[]{"shape"})
                .build();

        map.put("shape", shapeMapping);

        ret.put(tensorflowName(), map);
        ret.put(onnxName(), map);

        return ret;
    }


    @Override
    public String opName() {
        return "reshape";
    }

    @Override
    public String onnxName() {
        return "Reshape";
    }

    @Override
    public String tensorflowName() {
        return "Reshape";
    }



    @Override
    public void configureFromArguments() {
        if(iArguments.size() > 1) {
            //ordering comes first followed by the actual shape
            this.shape = new long[iArguments.size() - 1];
            for(int i = 0; i < shape.length; i++) {
                this.shape[i] = iArguments.get(i + 1);
            }
        }
    }

    @Override
    public void setPropertiesForFunction(Map properties) {
    }

    @Override
    public List doDiff(List i_v) {
        SDVariable origShape = sameDiff.shape(arg());
        SDVariable ret = sameDiff.reshape(i_v.get(0), origShape);
        return Collections.singletonList(ret);
    }

    @Override
    public List calculateOutputDataTypes(List dataTypes){
        //Output type is always same as input type
        return Collections.singletonList(dataTypes.get(0));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy