public override KerasSymbol[] Invoke(KerasSymbol[] inputs, FuncArgs kwargs = null) { List <KerasSymbol> result = new List <KerasSymbol>(); foreach (var input in inputs) { if (this.data_format == "channels_first") { // Ensure works for any dim var permutation = new List <int> { 0 }; permutation.AddRange((from i in Enumerable.Range(2, K.NDim(input) - 2) select i).ToList()); permutation.Add(1); result.Add(K.PermuteDimensions(input, new Shape(permutation))); } else { result.Add(K.BatchFlatten(input)); } } return(result.ToArray()); }
public static KerasSymbol Softmax(KerasSymbol x, int axis = -1) { var ndim = K.NDim(x); if (ndim == 2) { return(K.Softmax(x)); } else if (ndim > 2) { var e = K.Exp(x - K.Max(x, axis: axis, keepdims: true)); var s = K.Sum(e, axis: axis, keepdims: true); return(e / s); } else if (ndim == 0) { // x dim is not inferred yet return(K.Softmax(x)); } else { throw new Exception($"Cannot apply softmax to a tensor that is 1D. Received input: {x}"); } }