Skip to content

Commit b81eba9

Browse files
cesarsouzamigueldeicaza
authored andcommitted
Updating TypeFromTensorType to be public and adding a related TensorTypeFromType method to perform the opposite operation. (migueldeicaza#125)
Also adding some small tuning to editorconfig to make sure switch blocks are formatted correctly. - Updates migueldeicazaGH-116: Converting to and from TFDataType and System.Type
1 parent 30314fd commit b81eba9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ root = true
33
[*.cs]
44
indent_style = tab
55
indent_size = 4
6+
trim_trailing_whitespace = false
67
csharp_space_before_open_square_brackets = true
78
csharp_space_between_method_call_name_and_opening_parenthesis = true
89
csharp_space_between_method_declaration_name_and_open_parenthesis = true
910
csharp_new_line_before_else = false
11+
csharp_indent_switch_labels = false
1012
csharp_new_line_before_open_brace = types,methods,properties,events
1113
# other valid values:
1214
# - types

TensorFlowSharp/Tensor.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,12 @@ public long [] Shape {
751751
}
752752
}
753753

754-
static Type TypeFromTensorType (TFDataType type)
754+
/// <summary>
755+
/// Converts a <see cref="TFDataType"/> to a system type.
756+
/// </summary>
757+
/// <param name="type">The <see cref="TFDataType"/> to be converted.</param>
758+
/// <returns>The system type corresponding to the given <paramref name="type"/>.</returns>
759+
public static Type TypeFromTensorType (TFDataType type)
755760
{
756761
switch (type) {
757762
case TFDataType.Float:
@@ -781,6 +786,39 @@ static Type TypeFromTensorType (TFDataType type)
781786
}
782787
}
783788

789+
/// <summary>
790+
/// Converts a system type to a <see cref="TFDataType"/>.
791+
/// </summary>
792+
/// <param name="type">The system type to be converted.</param>
793+
/// <returns>The <see cref="TFDataType"/> corresponding to the given type.</returns>
794+
public static TFDataType TensorTypeFromType (Type type)
795+
{
796+
if (type == typeof (float))
797+
return TFDataType.Float;
798+
if (type == typeof (double))
799+
return TFDataType.Double;
800+
if (type == typeof (int))
801+
return TFDataType.Int32;
802+
if (type == typeof (byte))
803+
return TFDataType.UInt8;
804+
if (type == typeof (short))
805+
return TFDataType.Int16;
806+
if (type == typeof (sbyte))
807+
return TFDataType.Int8;
808+
if (type == typeof (string))
809+
return TFDataType.String;
810+
if (type == typeof (long))
811+
return TFDataType.Int64;
812+
if (type == typeof (bool))
813+
return TFDataType.Bool;
814+
if (type == typeof (ushort))
815+
return TFDataType.UInt16;
816+
if (type == typeof (Complex))
817+
return TFDataType.Complex128;
818+
819+
throw new ArgumentOutOfRangeException (nameof(type), $"The given type could not be mapped to an existing {nameof(TFDataType)}.");
820+
}
821+
784822
static unsafe object FetchSimple (TFDataType dt, IntPtr data)
785823
{
786824
switch (dt) {

0 commit comments

Comments
 (0)