Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inconsistent shape error while training embedding layer #1125

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/TensorFlowNET.Core/Framework/IndexedSlices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,25 @@ public IndexedSlices(Tensor values, Tensor indices, Tensor dense_shape = null)

public static implicit operator Tensor(IndexedSlices indexedSlices)
{
return indexedSlices.values;
return _indexed_slices_to_tensor(indexedSlices);
}

public static implicit operator IndexedSlices(Tensor tensor)
{
return tensor.Tag as IndexedSlices;
}

/// <summary>
/// Converts an IndexedSlices object `value` to a Tensor.
/// </summary>
/// <param name="indexedSlices"></param>
/// <param name="dtype"></param>
/// <param name="name"></param>
/// <param name="as_ref"></param>
/// <returns></returns>
public static Tensor _indexed_slices_to_tensor(IndexedSlices indexedSlices, TF_DataType dtype = TF_DataType.DtInvalid, String name = "", bool as_ref = false)
{
return gen_math_ops.unsorted_segment_sum(indexedSlices.values, indexedSlices.indices, indexedSlices.dense_shape.slice(0));
}
}
}
11 changes: 11 additions & 0 deletions test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public void Embedding()
var output_array = model.predict(input_array);
Assert.AreEqual((32, 10, 64), output_array.shape);
}
[TestMethod]
public void EmbeddingGrad()
{
var inputs = keras.layers.Input(shape: new[] { 32, 10 });
var outputs = keras.layers.Embedding(1000, 64, input_length: 10).Apply(inputs);
var model = keras.Model(inputs: inputs, outputs: outputs);
var input_array = np.random.randint(1000, size: (1, 32, 10));
var output_array = np.random.random(size: (1, 32, 10, 64));
model.compile("rmsprop", "mse", new[] { "accuracy" });
model.fit(input_array, output_array);
}

/// <summary>
/// https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
Expand Down
Loading