/** * Gets rid of any singleton dimensions of the given array * * @param shape the shape to squeeze * @return the array with all of the singleton dimensions removed */ public static int[] squeeze(int[] shape) { if (isColumnVectorShape(shape)) return shape; List<Integer> ret = new ArrayList<>(); //strip all but last dimension for (int i = 0; i < shape.length; i++) if (shape[i] != 1) ret.add(shape[i]); return ArrayUtil.toArray(ret); }
/** * Calculate the shape for the given set of indices. * <p/> * The shape is defined as (for each dimension) * the difference between the end index + 1 and * the begin index * * @param indices the indices to calculate the shape for * @return the shape for the given indices */ public static int[] shape(INDArrayIndex... indices) { int[] ret = new int[indices.length]; for (int i = 0; i < ret.length; i++) { // FIXME: LONG ret[i] = (int) indices[i].length(); } List<Integer> nonZeros = new ArrayList<>(); for (int i = 0; i < ret.length; i++) { if (ret[i] > 0) nonZeros.add(ret[i]); } return ArrayUtil.toArray(nonZeros); }
/** * Gets rid of any singleton dimensions of the given array * * @param shape the shape to squeeze * @return the array with all of the singleton dimensions removed */ public static int[] squeeze(int[] shape) { if (isColumnVectorShape(shape)) return shape; List<Integer> ret = new ArrayList<>(); //strip all but last dimension for (int i = 0; i < shape.length; i++) if (shape[i] != 1) ret.add(shape[i]); return ArrayUtil.toArray(ret); }
/** * Calculate the shape for the given set of indices. * <p/> * The shape is defined as (for each dimension) * the difference between the end index + 1 and * the begin index * * @param indices the indices to calculate the shape for * @return the shape for the given indices */ public static int[] shape(INDArrayIndex... indices) { int[] ret = new int[indices.length]; for (int i = 0; i < ret.length; i++) { // FIXME: LONG ret[i] = (int) indices[i].length(); } List<Integer> nonZeros = new ArrayList<>(); for (int i = 0; i < ret.length; i++) { if (ret[i] > 0) nonZeros.add(ret[i]); } return ArrayUtil.toArray(nonZeros); }
/** * Get (convolution) stride from Keras layer configuration. * * @param layerConfig dictionary containing Keras layer configuration * @return * @throws InvalidKerasConfigurationException */ public static int[] getStrideFromConfig(Map<String, Object> layerConfig) throws InvalidKerasConfigurationException { Map<String, Object> innerConfig = getInnerLayerConfigFromConfig(layerConfig); int[] strides = null; if (innerConfig.containsKey(LAYER_FIELD_SUBSAMPLE)) { /* Convolutional layers. */ List<Integer> stridesList = (List<Integer>) innerConfig.get(LAYER_FIELD_SUBSAMPLE); strides = ArrayUtil.toArray(stridesList); } else if (innerConfig.containsKey(LAYER_FIELD_STRIDES)) { /* Pooling layers. */ List<Integer> stridesList = (List<Integer>) innerConfig.get(LAYER_FIELD_STRIDES); strides = ArrayUtil.toArray(stridesList); } else throw new InvalidKerasConfigurationException("Could not determine layer stride: no " + LAYER_FIELD_SUBSAMPLE + " or " + LAYER_FIELD_STRIDES + " field found"); return strides; }
/** * Get (convolution) kernel size from Keras layer configuration. * * @param layerConfig dictionary containing Keras layer configuration * @return * @throws InvalidKerasConfigurationException */ public static int[] getKernelSizeFromConfig(Map<String, Object> layerConfig) throws InvalidKerasConfigurationException { Map<String, Object> innerConfig = getInnerLayerConfigFromConfig(layerConfig); int[] kernelSize = null; if (innerConfig.containsKey(LAYER_FIELD_NB_ROW) && innerConfig.containsKey(LAYER_FIELD_NB_COL)) { /* Convolutional layers. */ List<Integer> kernelSizeList = new ArrayList<Integer>(); kernelSizeList.add((Integer) innerConfig.get(LAYER_FIELD_NB_ROW)); kernelSizeList.add((Integer) innerConfig.get(LAYER_FIELD_NB_COL)); kernelSize = ArrayUtil.toArray(kernelSizeList); } else if (innerConfig.containsKey(LAYER_FIELD_POOL_SIZE)) { /* Pooling layers. */ List<Integer> kernelSizeList = (List<Integer>) innerConfig.get(LAYER_FIELD_POOL_SIZE); kernelSize = ArrayUtil.toArray(kernelSizeList); } else throw new InvalidKerasConfigurationException("Could not determine kernel size: no " + LAYER_FIELD_NB_ROW + ", " + LAYER_FIELD_NB_COL + ", or " + LAYER_FIELD_POOL_SIZE + " field found"); return kernelSize; }
int[] toReshape = ArrayUtil.toArray(newDims);