@Test public void testResolveNewShape3() { TensorShape shape = new TensorShape(new int[]{4, 28, 28, 1}); assertArrayEquals( new int[]{4, 28, 28, 1}, shape.resolveNewShape(new int[]{-1, -1, -1, 1}) ); assertArrayEquals( new int[]{4, 28, 28}, shape.resolveNewShape(new int[]{-1, -1, -1}) ); }
public int[] resolveNewShape(int[] newShape) throws EvaluationException { long negativeValues = Arrays.stream(newShape).filter(x -> x < 0).count(); int[] result = newShape; if (negativeValues == 1) { int thisProduct = this.shapeProduct(); int newShapeProduct = Arrays .stream(newShape).filter(x -> x > 0) .reduce(1, (x, y) -> x * y); int replacedValue = thisProduct / newShapeProduct; result = Arrays .stream(newShape) .map(x -> { if (x < 0) { return replacedValue; } else { return x; } }) .toArray(); } else if (negativeValues > 1) { result = new int[newShape.length]; for (int i = 0; i < newShape.length; i++) { int newShapeDimensionSize = newShape[i]; int currentShapeDimensionSize = 1; if (i < this.shape.length) { currentShapeDimensionSize = this.shape[i]; } if (newShapeDimensionSize < 0) { result[i] = currentShapeDimensionSize; } else { if (newShapeDimensionSize != currentShapeDimensionSize) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } result[i] = currentShapeDimensionSize; } } } if (!isCompatibleWith(result)) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } return result; }
TensorShape { public int[] resolveNewShape(int[] newShape) throws EvaluationException { long negativeValues = Arrays.stream(newShape).filter(x -> x < 0).count(); int[] result = newShape; if (negativeValues == 1) { int thisProduct = this.shapeProduct(); int newShapeProduct = Arrays .stream(newShape).filter(x -> x > 0) .reduce(1, (x, y) -> x * y); int replacedValue = thisProduct / newShapeProduct; result = Arrays .stream(newShape) .map(x -> { if (x < 0) { return replacedValue; } else { return x; } }) .toArray(); } else if (negativeValues > 1) { result = new int[newShape.length]; for (int i = 0; i < newShape.length; i++) { int newShapeDimensionSize = newShape[i]; int currentShapeDimensionSize = 1; if (i < this.shape.length) { currentShapeDimensionSize = this.shape[i]; } if (newShapeDimensionSize < 0) { result[i] = currentShapeDimensionSize; } else { if (newShapeDimensionSize != currentShapeDimensionSize) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } result[i] = currentShapeDimensionSize; } } } if (!isCompatibleWith(result)) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } return result; } }
TensorShape { public int[] resolveNewShape(int[] newShape) throws EvaluationException { long negativeValues = Arrays.stream(newShape).filter(x -> x < 0).count(); int[] result = newShape; if (negativeValues == 1) { int thisProduct = this.shapeProduct(); int newShapeProduct = Arrays .stream(newShape).filter(x -> x > 0) .reduce(1, (x, y) -> x * y); int replacedValue = thisProduct / newShapeProduct; result = Arrays .stream(newShape) .map(x -> { if (x < 0) { return replacedValue; } else { return x; } }) .toArray(); } else if (negativeValues > 1) { result = new int[newShape.length]; for (int i = 0; i < newShape.length; i++) { int newShapeDimensionSize = newShape[i]; int currentShapeDimensionSize = 1; if (i < this.shape.length) { currentShapeDimensionSize = this.shape[i]; } if (newShapeDimensionSize < 0) { result[i] = currentShapeDimensionSize; } else { if (newShapeDimensionSize != currentShapeDimensionSize) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } result[i] = currentShapeDimensionSize; } } } if (!isCompatibleWith(result)) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } return result; } TensorShape(int[] shape);  TensorShape(long[] shape); }
TensorShape { public int[] resolveNewShape(int[] newShape) throws EvaluationException { long negativeValues = Arrays.stream(newShape).filter(x -> x < 0).count(); int[] result = newShape; if (negativeValues == 1) { int thisProduct = this.shapeProduct(); int newShapeProduct = Arrays .stream(newShape).filter(x -> x > 0) .reduce(1, (x, y) -> x * y); int replacedValue = thisProduct / newShapeProduct; result = Arrays .stream(newShape) .map(x -> { if (x < 0) { return replacedValue; } else { return x; } }) .toArray(); } else if (negativeValues > 1) { result = new int[newShape.length]; for (int i = 0; i < newShape.length; i++) { int newShapeDimensionSize = newShape[i]; int currentShapeDimensionSize = 1; if (i < this.shape.length) { currentShapeDimensionSize = this.shape[i]; } if (newShapeDimensionSize < 0) { result[i] = currentShapeDimensionSize; } else { if (newShapeDimensionSize != currentShapeDimensionSize) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } result[i] = currentShapeDimensionSize; } } } if (!isCompatibleWith(result)) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } return result; } TensorShape(int[] shape);  TensorShape(long[] shape); boolean handleBatch(); int[] getNewShape(int batchSize, Integer replaceMissingBy); int[] getArrayShape(); TensorShape simplifyShape(); int[] resolveNewShape(int[] newShape); boolean isCompatibleWith(int[] otherShape); int shapeProduct(); int getRank(); boolean isOfRank(int rank); boolean isScalarShape(); boolean isVectorShape(); boolean isMatrixShape(); long numberOfUnknownDimensions(); }
TensorShape { public int[] resolveNewShape(int[] newShape) throws EvaluationException { long negativeValues = Arrays.stream(newShape).filter(x -> x < 0).count(); int[] result = newShape; if (negativeValues == 1) { int thisProduct = this.shapeProduct(); int newShapeProduct = Arrays .stream(newShape).filter(x -> x > 0) .reduce(1, (x, y) -> x * y); int replacedValue = thisProduct / newShapeProduct; result = Arrays .stream(newShape) .map(x -> { if (x < 0) { return replacedValue; } else { return x; } }) .toArray(); } else if (negativeValues > 1) { result = new int[newShape.length]; for (int i = 0; i < newShape.length; i++) { int newShapeDimensionSize = newShape[i]; int currentShapeDimensionSize = 1; if (i < this.shape.length) { currentShapeDimensionSize = this.shape[i]; } if (newShapeDimensionSize < 0) { result[i] = currentShapeDimensionSize; } else { if (newShapeDimensionSize != currentShapeDimensionSize) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } result[i] = currentShapeDimensionSize; } } } if (!isCompatibleWith(result)) { throw new EvaluationException( String.format( "Unable to resolve shape %s into %s", Arrays.toString(this.getArrayShape()), Arrays.toString(newShape) ) ); } return result; } TensorShape(int[] shape);  TensorShape(long[] shape); boolean handleBatch(); int[] getNewShape(int batchSize, Integer replaceMissingBy); int[] getArrayShape(); TensorShape simplifyShape(); int[] resolveNewShape(int[] newShape); boolean isCompatibleWith(int[] otherShape); int shapeProduct(); int getRank(); boolean isOfRank(int rank); boolean isScalarShape(); boolean isVectorShape(); boolean isMatrixShape(); long numberOfUnknownDimensions();  }