Repository navigation
Expand file tree
/
Copy pathMemoryBasicTest.cs
More file actions
152 lines (136 loc) · 5.36 KB
/
Copy pathMemoryBasicTest.cs
File metadata and controls
152 lines (136 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using NumSharp;
using System;
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine.DataAdapters;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
namespace Tensorflow
{
class MemoryBasicTest
{
public Action<int, int> Placeholder
=> (epoch, iterate) =>
{
var ph = array_ops.placeholder(tf.float32, (10, 512, 512, 3));
};
/// <summary>
///
/// </summary>
public Action<int, int> Constant
=> (epoch, iterate) =>
{
var tensor = tf.constant(3112.0f);
};
public Action<int, int> Constant2x3
=> (epoch, iterate) =>
{
var nd = np.arange(1000).reshape(10, 100);
var tensor = tf.constant(nd);
var data = tensor.numpy();
};
public Action<int, int> ConstantString
=> (epoch, iterate) =>
{
var tensor = tf.constant(new string[]
{
"Biden immigration bill would put millions of illegal immigrants on 8-year fast-track to citizenship",
"The Associated Press, which also reported that the eight-year path is in the bill.",
"The bill would also include provisions to stem the flow of migration by addressing root causes of migration from south of the border."
});
var data = tensor.numpy();
};
public Action<int, int> Variable
=> (epoch, iterate) =>
{
var nd = np.arange(1 * 256 * 256 * 3).reshape(1, 256, 256, 3);
ResourceVariable variable = tf.Variable(nd);
};
public Action<int, int> VariableRead
=> (epoch, iterate) =>
{
var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
ResourceVariable variable = tf.Variable(nd);
var nd2 = np.arange(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
variable.assign(nd2);
for (int i = 0; i< 100; i++)
{
var v = variable.numpy();
}
};
public Action<int, int> MathAdd
=> (epoch, iterate) =>
{
var x = tf.constant(3112.0f);
var y = tf.constant(3112.0f);
var z = x + y;
};
public Action<int, int> Gradient
=> (epoch, iterate) =>
{
var w = tf.constant(3112.0f);
using var tape = tf.GradientTape();
tape.watch(w);
var loss = w * w;
var grad = tape.gradient(loss, w);
};
public Action<int, int> Conv2DWithTensor
=> (epoch, iterate) =>
{
var input = array_ops.zeros((10, 32, 32, 3), dtypes.float32);
var filter = array_ops.zeros((3, 3, 3, 32), dtypes.float32);
var strides = new[] { 1, 1, 1, 1 };
var dilations = new[] { 1, 1, 1, 1 };
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
"Conv2D", null,
null,
input, filter,
"strides", strides,
"use_cudnn_on_gpu", true,
"padding", "VALID",
"explicit_paddings", new int[0],
"data_format", "NHWC",
"dilations", dilations);
};
public Action<int, int> Conv2DWithVariable
=> (epoch, iterate) =>
{
var input = array_ops.zeros((10, 32, 32, 3), dtypes.float32);
var filter = tf.Variable(array_ops.zeros((3, 3, 3, 32), dtypes.float32));
var strides = new[] { 1, 1, 1, 1 };
var dilations = new[] { 1, 1, 1, 1 };
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
"Conv2D", null,
null,
input, filter,
"strides", strides,
"use_cudnn_on_gpu", true,
"padding", "VALID",
"explicit_paddings", new int[0],
"data_format", "NHWC",
"dilations", dilations);
};
public Action<int, int> Dataset
=> (epoch, iterate) =>
{
TensorShape shape = (16, 32, 32, 3);
var images = np.arange(shape.size).astype(np.float32).reshape(shape.dims);
var data_handler = new DataHandler(new DataHandlerArgs
{
X = images,
BatchSize = 2,
StepsPerEpoch = -1,
InitialEpoch = 0,
Epochs = 2,
MaxQueueSize = 10,
Workers = 1,
UseMultiprocessing = false,
StepsPerExecution = tf.Variable(1)
});
/*foreach (var (_epoch, iterator) in data_handler.enumerate_epochs())
{
foreach (var step in data_handler.steps())
iterator.next();
}*/
};
}
}