Skip to content

Commit

Permalink
Working on GH-7: Make the first example for a Sequential model pass
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Aug 16, 2017
1 parent aad0c5b commit 222534b
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 124 deletions.
43 changes: 24 additions & 19 deletions Sources/Backends/Base/IBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public interface IBackend : IDisposable

Tensor square(Tensor w);

Tensor sum(Tensor v, int axis, bool keepdims);
Tensor sum(Tensor x, int[] axis = null, bool keepdims = false, string name = null);

Tensor sum(Tensor x, int axis, bool keepdims = false, string name = null);



Tensor clip(Tensor norms, int v, int maxValue);

Expand All @@ -54,7 +58,8 @@ public interface IBackend : IDisposable

Tensor greater_equal(Tensor w, double v);
void clear_session();
Tensor cast(object v1, object v2);

Tensor cast(Tensor x, TFDataType dataType);

Tensor dropout(object p, double retain_prob, object noise_shape, object seed);

Expand All @@ -69,13 +74,13 @@ public interface IBackend : IDisposable

Tensor max(Tensor x, int axis, bool keepdims);

Tensor div(Tensor e, Tensor s);

Tensor elu(Tensor x);

Tensor hard_sigmoid(Tensor x);


Tensor mul(Tensor a, Tensor b);

Tensor mul<T>(T a, Tensor b);

Expand All @@ -85,6 +90,17 @@ public interface IBackend : IDisposable




Tensor div(Tensor a, Tensor b);

Tensor div<T>(T a, Tensor b);

Tensor div<T>(Tensor a, T b);





Tensor add(Tensor a, Tensor b);

Tensor add<T>(Tensor a, T b);
Expand Down Expand Up @@ -115,17 +131,14 @@ public interface IBackend : IDisposable

Tensor exp(object v);

Tensor div(Tensor desired, object v);

object eval(Tensor tensor);



Tensor mul(Tensor w, Tensor tensor);


Tensor clip(Tensor norms, double min_value, double max_value);



Tensor random_uniform(int?[] shape, double minvalue = 0.0, double maxvalue = 1.0, TFDataType dtype = Utils.DEFAULT_DTYPE, int? seed = null, string name = null);

Expand All @@ -135,16 +148,14 @@ public interface IBackend : IDisposable

Tensor minus(Tensor tensor);

Tensor mean(Tensor tensor, int axis = -1);
Tensor mean(Tensor tensor, int axis = -1, bool keepdims = false, string name = null);

Tensor mean(Tensor tensor, int[] axis);
Tensor mean(Tensor tensor, int[] axis, bool keepdims = false, string name = null);

Tensor abs(Tensor input);

Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false);

Tensor sum(Tensor tensor, int axis);

Tensor max(Tensor tensor, int axis);

Tensor maximum(double v, Tensor tensor);
Expand Down Expand Up @@ -179,8 +190,6 @@ public interface IBackend : IDisposable

IDisposable name_scope(string name);

object sum(Tensor tensor);

Tensor clip_norm(Tensor g, double clipnorm, Tensor norm);

Tensor identity(Tensor x);
Expand All @@ -196,8 +205,6 @@ public interface IBackend : IDisposable

int?[] int_shape(TFTensor input_tensor);

Tensor div(double v1, object v2);

List<Array> batch_get_value(List<List<Tensor>> weights);

void batch_set_value(List<(Tensor, Array)> tuples);
Expand All @@ -209,7 +216,7 @@ public interface IBackend : IDisposable

Tensor get_variable_shape(object s);

object sum(double v, Tensor tensor);
Tensor sum(double v, Tensor tensor);

bool is_sparse(Tensor tensor);

Expand All @@ -227,8 +234,6 @@ public interface IBackend : IDisposable

Tensor update(object m, object v);

Tensor div(Tensor tensor, int samples);

Tensor truncated_normal(int[] shape, double v, double stddev, TFDataType dtype, int? seed);

Tensor truncated_normal(int?[] shape, double v, double stddev, TFDataType dtype, int? seed);
Expand Down
139 changes: 95 additions & 44 deletions Sources/Backends/TensorFlowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public Tensor binary_crossentropy(Tensor expected, Tensor actual)
throw new NotImplementedException();
}

public Tensor cast(object v1, object v2)
public Tensor cast(Tensor x, TFDataType dataType)
{
throw new NotImplementedException();
return tensor(tf.Cast(x, dataType));
}

/// <summary>
Expand All @@ -187,18 +187,17 @@ public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_l
if (!from_logits)
{
// scale preds so that the class probas of each sample sum to 1
var shape = output.shape;
var o = output.output;
o = tf.Div(o, tf.ReduceSum(output.output, axis: tf.Const(new TFTensor(shape.Length - 1)), keep_dims: true));
int?[] shape = output.shape;
var last = tf.Const(new TFTensor(shape.Length - 1));
TFOutput o = tf.Div(output, tf.ReduceSum(output, axis: last, keep_dims: true));
// manual computation of crossentropy
var _epsilon = constant(epsilon(), dtype: output.dtype);
//output = tf.clip_by_value(output, _epsilon, 1.0 - _epsilon);
//return -tf.reduce_sum(target * tf.log(output), axis: len(output.get_shape()) - 1);
TFOutput _epsilon = constant(epsilon(), dtype: output.dtype);
o = tf.ClipByValue(o, _epsilon, tf.Sub(constant(1f), _epsilon));
o = tf.Neg(tf.ReduceSum(tf.Mul(target, tf.Log(output)), axis: last));
return tensor(o);
}

//return tf.softmax_cross_entropy_with_logits(labels: target, logits: output);

throw new NotImplementedException();
return tensor(tf.SoftmaxCrossEntropyWithLogits(target, output).loss);
}

public Tensor clip(Tensor norms, int v, int maxValue)
Expand Down Expand Up @@ -228,25 +227,8 @@ public Tensor constant<T>(T value, int?[] shape = null, TFDataType? dtype = null
}


public Tensor div(Tensor e, Tensor s)
{
throw new NotImplementedException();
}

public Tensor div(Tensor desired, object v)
{
throw new NotImplementedException();
}

public Tensor div(double v1, object v2)
{
throw new NotImplementedException();
}

public Tensor div(Tensor tensor, int samples)
{
throw new NotImplementedException();
}

public Tensor dropout(object p, double retain_prob, object noise_shape, object seed)
{
Expand Down Expand Up @@ -336,9 +318,14 @@ public Tensor greater_equal(Tensor w, double v)
throw new NotImplementedException();
}

public Tensor not_equal(Tensor w, double v)
public Tensor not_equal(Tensor x, Tensor y)
{
throw new NotImplementedException();
return tensor(tf.NotEqual(x, y));
}

public Tensor not_equal(Tensor x, double y)
{
return tensor(tf.NotEqual(x, tf.Const(y, x.dtype)));
}

public Tensor hard_sigmoid(Tensor x)
Expand Down Expand Up @@ -422,24 +409,69 @@ public Tensor maximum(double v, Tensor tensor)
throw new NotImplementedException();
}

public Tensor mean(Tensor tensor, int axis)


public TFOutput _normalize_axis(int[] axis, int? ndim)
{
throw new NotImplementedException();
axis = (int[])axis.Clone();
for (int i = 0; i < axis.Length; i++)
{
if (axis[i] < 0)
axis[i] = axis[i] % ndim.Value;
}

return tf.Const(axis);
}

public Tensor mean(Tensor tensor, int[] axis)
/// <summary>
/// Mean of a tensor, alongside the specified axis.
/// </summary>
///
/// <param name="x">A tensor or variable.</param>
/// <param name="axis">A list of integer. Axes to compute the mean.</param>
/// <param name="keepdims>A boolean, whether to keep the dimensions or not. If <paramref name="keepdims"/> is <c>false</c>,
/// the rank of the tensor is reduced by 1 for each entry in <paramref name="axis"/>. If <paramref name="keepdims"/> is
/// <c>true</c>, the reduced dimensions are retained with length 1.
///
/// <returns>A tensor with the mean of elements of <c>x</c>.</returns>
///
public Tensor mean(Tensor x, int[] axis, bool keepdims = false, string name = null)
{
throw new NotImplementedException();
return tensor(tf.ReduceMean(x, _normalize_axis(axis, ndim(x)), keepdims, operName: name));
}

/// <summary>
/// Mean of a tensor, alongside the specified axis.
/// </summary>
///
/// <param name="x">A tensor or variable.</param>
/// <param name="axis">The axis where to compute the mean.</param>
/// <param name="keepdims>A boolean, whether to keep the dimensions or not. If <paramref name="keepdims"/> is <c>false</c>,
/// the rank of the tensor is reduced by 1 for each entry in <paramref name="axis"/>. If <paramref name="keepdims"/> is
/// <c>true</c>, the reduced dimensions are retained with length 1.
///
/// <returns>A tensor with the mean of elements of <c>x</c>.</returns>
///
public Tensor mean(Tensor x, int axis = -1, bool keepdims = false, string name = null)
{
return tensor(tf.ReduceMean(x, axis: tf.Const(axis), keep_dims: keepdims, operName: name));
}


public Tensor minus(Tensor tensor)
{
throw new NotImplementedException();
}

public Tensor dot(Tensor a, Tensor b)
{
return tensor(tf.MatMul(a.output, b.output));
}


public Tensor mul<T>(T a, Tensor b)
{
return mul(constant(a), b);
return mul(constant(a, dtype: b.dtype), b);
}

public Tensor mul(Tensor a, Tensor b)
Expand All @@ -449,21 +481,40 @@ public Tensor mul(Tensor a, Tensor b)

public Tensor mul<T>(Tensor a, T b)
{
return mul(a, constant(b));
return mul(a, constant(b, dtype: a.dtype));
}

public Tensor mul(List<Tensor> batch_outs, int length)
{
throw new NotImplementedException();
}

public Tensor dot(Tensor a, Tensor b)




public Tensor div<T>(T a, Tensor b)
{
return tensor(tf.MatMul(a.output, b.output));
return div(constant(a, dtype: b.dtype), b);
}

public Tensor mul(List<Tensor> batch_outs, int length)
public Tensor div(Tensor a, Tensor b)
{
return tensor(tf.Mul(a.output, b.output));
}

public Tensor div<T>(Tensor a, T b)
{
return div(a, constant(b, dtype: a.dtype));
}

public Tensor div(List<Tensor> batch_outs, int length)
{
throw new NotImplementedException();
}



public Tensor add(Tensor a, Tensor b)
{
return tensor(tf.Add(a.output, b.output));
Expand Down Expand Up @@ -608,14 +659,14 @@ public Tensor square(Tensor w)
throw new NotImplementedException();
}

public Tensor sum(Tensor v, int axis, bool keepdims)
public Tensor sum(Tensor x, int[] axis, bool keepdims = false, string name = null)
{
throw new NotImplementedException();
return tensor(tf.ReduceSum(x, tf.Const(axis), keepdims, name));
}

public Tensor sum(Tensor tensor, int axis)
public Tensor sum(Tensor x, int axis, bool keepdims = false, string name = null)
{
throw new NotImplementedException();
return tensor(tf.ReduceSum(x, tf.Const(axis), keepdims, name));
}

public object sum(object[] v)
Expand All @@ -628,7 +679,7 @@ public object sum(Tensor tensor)
throw new NotImplementedException();
}

public object sum(double v, Tensor tensor)
public Tensor sum(double v, Tensor tensor)
{
throw new NotImplementedException();
}
Expand Down
Loading

0 comments on commit 222534b

Please sign in to comment.