public static NDArray StandardizeSingleArray(NDArray x) { if (x == null) { return(null); } else if (K.IsTensor(x)) { var shape = x.Shape; if (shape == null || shape[0] == 0) { throw new Exception(String.Format("When feeding symbolic tensors to a model, we expect thetensors to have a static batch size. Got tensor with shape: {0}", shape.ToString())); } return(x); } else if (x.Dimension == 1) { x = nd.ExpandDims(x, 1); } return(x); }
public static NDArrayList StandardizeInputData(NDArrayList data, string[] names, Shape[] shapes = null, bool check_batch_axis = true, string exception_prefix = "", bool check_last_layer_shape = true) { if (names == null) { if (data != null && data.Length == 0) { throw new Exception("Error when checking model " + exception_prefix + ": expected no data"); } return(new NDArrayList()); } if (data == null) { return((from _ in Enumerable.Range(0, names.Length) select new NDArray()).ToList()); } data = (from x in data select StandardizeSingleArray(x)).ToList(); if (data.Length != names.Length) { if (data[0].Shape != null) { throw new Exception("Error when checking model " + exception_prefix + ": the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see " + names.Length.ToString() + " array(s), but instead got the following list of " + data.Length.ToString()); } else if (names.Length > 1) { throw new Exception("Error when checking model " + exception_prefix + ": you are passing a list as input to your model, but the model expects a list of " + names.Length.ToString()); } } // Check shapes compatibility. if (shapes != null) { foreach (var i in Enumerable.Range(0, names.Length)) { if (shapes[i] != null && !K.IsTensor(data[i])) { var data_shape = data[i].Shape; var shape = shapes[i]; if (data[i].Dimension != shape.Dimension) { throw new Exception("Error when checking " + exception_prefix + ": expected " + names[i] + " to have " + shape.Dimension.ToString() + " dimensions, but got array with shape " + data_shape.ToString()); } if (!check_batch_axis) { data_shape = data_shape[1]; shape = shape[1]; } foreach (var _tup_1 in Enumerable.Zip(data_shape.Data, shape.Data, (d, s) => { return(d, s); })) { var dim = _tup_1.Item1; var ref_dim = _tup_1.Item2; if (ref_dim != dim && ref_dim > 0) { // ignore shape difference in last layer only if loss is // multi_hot_sparse_categorical_crossentropy, // last layer can only be dense or activation layer if (!check_last_layer_shape && (names[i].ToLower().StartsWith("dense") || names[i].ToLower().StartsWith("activation"))) { continue; } throw new Exception("Error when checking " + exception_prefix + ": expected " + names[i] + " to have shape " + shape.ToString() + " but got array with shape " + data_shape.ToString()); } } } } } return(data); }