开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (30)
标签 (6)
master
v0.15-tensorflow1.15
v0.20-tensorflow2.0
sharwell/renormalize
v0.14-maintenance
v0.13-lstm
singlethreaded-support
tensor-conversions
v0.12-rnn
exception-swallow-issue404
numsharp-upgrade-version
unknown-dimension
keras-activations
autocast-tensor
tensor-operators
multithreading
gc-deallocation
perf-ops
v0.11-maintenance
revert-353-add-tf-nn-in_top_k
v0.13
v0.12
v0.10
v0.9
v0.8.0
v0.1.0-Tensor
master
分支 (30)
标签 (6)
master
v0.15-tensorflow1.15
v0.20-tensorflow2.0
sharwell/renormalize
v0.14-maintenance
v0.13-lstm
singlethreaded-support
tensor-conversions
v0.12-rnn
exception-swallow-issue404
numsharp-upgrade-version
unknown-dimension
keras-activations
autocast-tensor
tensor-operators
multithreading
gc-deallocation
perf-ops
v0.11-maintenance
revert-353-add-tf-nn-in_top_k
v0.13
v0.12
v0.10
v0.9
v0.8.0
v0.1.0-Tensor
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (30)
标签 (6)
master
v0.15-tensorflow1.15
v0.20-tensorflow2.0
sharwell/renormalize
v0.14-maintenance
v0.13-lstm
singlethreaded-support
tensor-conversions
v0.12-rnn
exception-swallow-issue404
numsharp-upgrade-version
unknown-dimension
keras-activations
autocast-tensor
tensor-operators
multithreading
gc-deallocation
perf-ops
v0.11-maintenance
revert-353-add-tf-nn-in_top_k
v0.13
v0.12
v0.10
v0.9
v0.8.0
v0.1.0-Tensor
GraphTest.cs 17.25 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using Tensorflow;
using Buffer = Tensorflow.Buffer;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest
{
[TestClass]
public class GraphTest : CApiTest
{
/// <summary>
/// Port from c_api_test.cc
/// `TEST(CAPI, Graph)`
/// </summary>
[TestMethod]
public void Graph()
{
var s = new Status();
var graph = new Graph();
// Make a placeholder operation.
var feed = c_test_util.Placeholder(graph, s);
EXPECT_EQ("feed", feed.name);
EXPECT_EQ("Placeholder", feed.OpType);
EXPECT_EQ("", feed.Device);
EXPECT_EQ(1, feed.NumOutputs);
EXPECT_EQ(TF_DataType.TF_INT32, feed.OutputType(0));
EXPECT_EQ(1, feed.OutputListLength("output"));
EXPECT_EQ(0, feed.NumInputs);
EXPECT_EQ(0, feed.OutputNumConsumers(0));
EXPECT_EQ(0, feed.NumControlInputs);
EXPECT_EQ(0, feed.NumControlOutputs);
AttrValue attr_value = null;
ASSERT_TRUE(c_test_util.GetAttrValue(feed, "dtype", ref attr_value, s));
EXPECT_EQ(attr_value.Type, DataType.DtInt32);
// Test not found errors in TF_Operation*() query functions.
EXPECT_EQ(-1, c_api.TF_OperationOutputListLength(feed, "bogus", s));
EXPECT_EQ(TF_Code.TF_INVALID_ARGUMENT, s.Code);
Assert.IsFalse(c_test_util.GetAttrValue(feed, "missing", ref attr_value, s));
EXPECT_EQ("Operation 'feed' has no attr named 'missing'.", s.Message);
// Make a constant oper with the scalar "3".
var three = c_test_util.ScalarConst(3, graph, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
// Add oper.
var add = c_test_util.Add(feed, three, graph, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
// Test TF_Operation*() query functions.
EXPECT_EQ("add", add.name);
EXPECT_EQ("AddN", add.OpType);
EXPECT_EQ("", add.Device);
EXPECT_EQ(1, add.NumOutputs);
EXPECT_EQ(TF_DataType.TF_INT32, add.OutputType(0));
EXPECT_EQ(1, add.OutputListLength("sum"));
EXPECT_EQ(TF_Code.TF_OK, s.Code);
EXPECT_EQ(2, add.InputListLength("inputs"));
EXPECT_EQ(TF_Code.TF_OK, s.Code);
EXPECT_EQ(TF_DataType.TF_INT32, add.InputType(0));
EXPECT_EQ(TF_DataType.TF_INT32, add.InputType(1));
var add_in_0 = add.Input(0);
EXPECT_EQ(feed, add_in_0.oper);
EXPECT_EQ(0, add_in_0.index);
var add_in_1 = add.Input(1);
EXPECT_EQ(three, add_in_1.oper);
EXPECT_EQ(0, add_in_1.index);
EXPECT_EQ(0, add.OutputNumConsumers(0));
EXPECT_EQ(0, add.NumControlInputs);
EXPECT_EQ(0, add.NumControlOutputs);
ASSERT_TRUE(c_test_util.GetAttrValue(add, "T", ref attr_value, s));
EXPECT_EQ(DataType.DtInt32, attr_value.Type);
ASSERT_TRUE(c_test_util.GetAttrValue(add, "N", ref attr_value, s));
EXPECT_EQ(2, (int)attr_value.I);
// Placeholder oper now has a consumer.
EXPECT_EQ(1, feed.OutputNumConsumers(0));
TF_Input[] feed_port = feed.OutputConsumers(0, 1);
EXPECT_EQ(1, feed_port.Length);
EXPECT_EQ(add, feed_port[0].oper);
EXPECT_EQ(0, feed_port[0].index);
// The scalar const oper also has a consumer.
EXPECT_EQ(1, three.OutputNumConsumers(0));
TF_Input[] three_port = three.OutputConsumers(0, 1);
EXPECT_EQ(add, three_port[0].oper);
EXPECT_EQ(1, three_port[0].index);
// Serialize to GraphDef.
var graph_def = c_test_util.GetGraphDef(graph);
// Validate GraphDef is what we expect.
bool found_placeholder = false;
bool found_scalar_const = false;
bool found_add = false;
foreach (var n in graph_def.Node)
{
if (c_test_util.IsPlaceholder(n))
{
Assert.IsFalse(found_placeholder);
found_placeholder = true;
}
else if (c_test_util.IsScalarConst(n, 3))
{
Assert.IsFalse(found_scalar_const);
found_scalar_const = true;
}
else if (c_test_util.IsAddN(n, 2))
{
Assert.IsFalse(found_add);
found_add = true;
}
else
{
Assert.Fail($"Unexpected NodeDef: {n}");
}
}
ASSERT_TRUE(found_placeholder);
ASSERT_TRUE(found_scalar_const);
ASSERT_TRUE(found_add);
// Add another oper to the graph.
var neg = c_test_util.Neg(add, graph, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
// Serialize to NodeDef.
var node_def = neg.node_def;
// Validate NodeDef is what we expect.
ASSERT_TRUE(c_test_util.IsNeg(node_def, "add"));
// Serialize to GraphDef.
var graph_def2 = c_test_util.GetGraphDef(graph);
// Compare with first GraphDef + added NodeDef.
graph_def.Node.Add(node_def);
EXPECT_EQ(graph_def, graph_def2);
// Look up some nodes by name.
Operation neg2 = c_api.TF_GraphOperationByName(graph, "neg");
EXPECT_EQ(neg, neg2);
var node_def2 = neg2.node_def;
EXPECT_EQ(node_def, node_def2);
Operation feed2 = c_api.TF_GraphOperationByName(graph, "feed");
EXPECT_EQ(feed, feed2);
node_def = feed.node_def;
node_def2 = feed2.node_def;
EXPECT_EQ(node_def, node_def2);
// Test iterating through the nodes of a graph.
found_placeholder = false;
found_scalar_const = false;
found_add = false;
bool found_neg = false;
uint pos = 0;
Operation oper;
while ((oper = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero)
{
if (oper.Equals(feed))
{
Assert.IsFalse(found_placeholder);
found_placeholder = true;
}
else if (oper.Equals(three))
{
Assert.IsFalse(found_scalar_const);
found_scalar_const = true;
}
else if (oper.Equals(add))
{
Assert.IsFalse(found_add);
found_add = true;
}
else if (oper.Equals(neg))
{
Assert.IsFalse(found_neg);
found_neg = true;
}
else
{
node_def = oper.node_def;
Assert.Fail($"Unexpected Node: {node_def.ToString()}");
}
}
ASSERT_TRUE(found_placeholder);
ASSERT_TRUE(found_scalar_const);
ASSERT_TRUE(found_add);
ASSERT_TRUE(found_neg);
graph.Dispose();
s.Dispose();
}
/// <summary>
/// Port from c_api_test.cc
/// `TEST(CAPI, ImportGraphDef)`
/// </summary>
[TestMethod]
public void ImportGraphDef()
{
var s = new Status();
var graph = new Graph().as_default();
// Create a simple graph.
c_test_util.Placeholder(graph, s);
var oper = c_test_util.ScalarConst(3, graph, s);
c_test_util.Neg(oper, graph, s);
// Export to a GraphDef.
var graph_def = new Buffer();
c_api.TF_GraphToGraphDef(graph, graph_def, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
// Import it, with a prefix, in a fresh graph.
graph.Dispose();
graph = new Graph().as_default();
var opts = c_api.TF_NewImportGraphDefOptions();
c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported");
c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
Operation scalar = graph.OperationByName("imported/scalar");
Operation feed = graph.OperationByName("imported/feed");
Operation neg = graph.OperationByName("imported/neg");
// Test basic structure of the imported graph.
EXPECT_EQ(0, scalar.NumInputs);
EXPECT_EQ(0, feed.NumInputs);
EXPECT_EQ(1, neg.NumInputs);
var neg_input = neg.Input(0);
EXPECT_EQ(scalar, neg_input.oper);
EXPECT_EQ(0, neg_input.index);
// Test that we can't see control edges involving the source and sink nodes.
EXPECT_EQ(0, scalar.NumControlInputs);
EXPECT_EQ(0, scalar.GetControlInputs().Length);
EXPECT_EQ(0, scalar.NumControlOutputs);
EXPECT_EQ(0, scalar.GetControlOutputs().Length);
EXPECT_EQ(0, feed.NumControlInputs);
EXPECT_EQ(0, feed.GetControlInputs().Length);
EXPECT_EQ(0, feed.NumControlOutputs);
EXPECT_EQ(0, feed.GetControlOutputs().Length);
EXPECT_EQ(0, neg.NumControlInputs);
EXPECT_EQ(0, neg.GetControlInputs().Length);
EXPECT_EQ(0, neg.NumControlOutputs);
EXPECT_EQ(0, neg.GetControlOutputs().Length);
// Import it again, with an input mapping, return outputs, and a return
// operation, into the same graph.
c_api.TF_DeleteImportGraphDefOptions(opts);
opts = c_api.TF_NewImportGraphDefOptions();
c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported2");
c_api.TF_ImportGraphDefOptionsAddInputMapping(opts, "scalar", 0, new TF_Output(scalar, 0));
c_api.TF_ImportGraphDefOptionsAddReturnOutput(opts, "feed", 0);
c_api.TF_ImportGraphDefOptionsAddReturnOutput(opts, "scalar", 0);
EXPECT_EQ(2, c_api.TF_ImportGraphDefOptionsNumReturnOutputs(opts));
c_api.TF_ImportGraphDefOptionsAddReturnOperation(opts, "scalar");
EXPECT_EQ(1, c_api.TF_ImportGraphDefOptionsNumReturnOperations(opts));
var results = c_api.TF_GraphImportGraphDefWithResults(graph, graph_def, opts, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
Operation scalar2 = graph.OperationByName("imported2/scalar");
Operation feed2 = graph.OperationByName("imported2/feed");
Operation neg2 = graph.OperationByName("imported2/neg");
// Check input mapping
neg_input = neg.Input(0);
EXPECT_EQ(scalar, neg_input.oper);
EXPECT_EQ(0, neg_input.index);
// Check return outputs
var return_outputs = graph.ReturnOutputs(results);
ASSERT_EQ(2, return_outputs.Length);
EXPECT_EQ(feed2, return_outputs[0].oper);
EXPECT_EQ(0, return_outputs[0].index);
EXPECT_EQ(scalar, return_outputs[1].oper); // remapped
EXPECT_EQ(0, return_outputs[1].index);
// Check return operation
var return_opers = graph.ReturnOperations(results);
ASSERT_EQ(1, return_opers.Length);
EXPECT_EQ(scalar2, return_opers[0]); // not remapped
c_api.TF_DeleteImportGraphDefResults(results);
// Import again, with control dependencies, into the same graph.
c_api.TF_DeleteImportGraphDefOptions(opts);
opts = c_api.TF_NewImportGraphDefOptions();
c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported3");
c_api.TF_ImportGraphDefOptionsAddControlDependency(opts, feed);
c_api.TF_ImportGraphDefOptionsAddControlDependency(opts, feed2);
c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
var scalar3 = graph.OperationByName("imported3/scalar");
var feed3 = graph.OperationByName("imported3/feed");
var neg3 = graph.OperationByName("imported3/neg");
ASSERT_TRUE(scalar3 != IntPtr.Zero);
ASSERT_TRUE(feed3 != IntPtr.Zero);
ASSERT_TRUE(neg3 != IntPtr.Zero);
// Check that newly-imported scalar and feed have control deps (neg3 will
// inherit them from input)
var control_inputs = scalar3.GetControlInputs();
ASSERT_EQ(2, scalar3.NumControlInputs);
EXPECT_EQ(feed, control_inputs[0]);
EXPECT_EQ(feed2, control_inputs[1]);
control_inputs = feed3.GetControlInputs();
ASSERT_EQ(2, feed3.NumControlInputs);
EXPECT_EQ(feed, control_inputs[0]);
EXPECT_EQ(feed2, control_inputs[1]);
// Export to a graph def so we can import a graph with control dependencies
graph_def = new Buffer();
c_api.TF_GraphToGraphDef(graph, graph_def, s);
EXPECT_EQ(TF_Code.TF_OK, s.Code);
// Import again, with remapped control dependency, into the same graph
c_api.TF_DeleteImportGraphDefOptions(opts);
opts = c_api.TF_NewImportGraphDefOptions();
c_api.TF_ImportGraphDefOptionsSetPrefix(opts, "imported4");
c_api.TF_ImportGraphDefOptionsRemapControlDependency(opts, "imported/feed", feed);
c_api.TF_GraphImportGraphDef(graph, graph_def, opts, s);
ASSERT_EQ(TF_Code.TF_OK, s.Code);
var scalar4 = graph.OperationByName("imported4/imported3/scalar");
var feed4 = graph.OperationByName("imported4/imported2/feed");
// Check that imported `imported3/scalar` has remapped control dep from
// original graph and imported control dep
control_inputs = scalar4.GetControlInputs();
ASSERT_EQ(2, scalar4.NumControlInputs);
EXPECT_EQ(feed, control_inputs[0]);
EXPECT_EQ(feed4, control_inputs[1]);
c_api.TF_DeleteImportGraphDefOptions(opts);
// Can add nodes to the imported graph without trouble.
c_test_util.Add(feed, scalar, graph, s);
ASSERT_EQ(TF_Code.TF_OK, s.Code);
}
/// <summary>
/// Port from c_api_test.cc
/// `TEST(CAPI, ImportGraphDef_WithReturnOutputs)`
/// </summary>
[TestMethod]
public void ImportGraphDef_WithReturnOutputs()
{
var s = new Status();
var graph = new Graph().as_default();
// Create a graph with two nodes: x and 3
c_test_util.Placeholder(graph, s);
ASSERT_TRUE(graph.OperationByName("feed") != null);
var oper = c_test_util.ScalarConst(3, graph, s);
ASSERT_TRUE(graph.OperationByName("scalar") != null);
c_test_util.Neg(oper, graph, s);
ASSERT_TRUE(graph.OperationByName("neg") != null);
// Export to a GraphDef.
var graph_def = graph.ToGraphDef(s);
ASSERT_EQ(TF_Code.TF_OK, s.Code);
// Import it in a fresh graph with return outputs.
graph.Dispose();
graph = new Graph().as_default();
var opts = new ImportGraphDefOptions();
opts.AddReturnOutput("feed", 0);
opts.AddReturnOutput("scalar", 0);
EXPECT_EQ(2, opts.NumReturnOutputs);
var return_outputs = graph.ImportGraphDefWithReturnOutputs(graph_def, opts, s);
ASSERT_EQ(TF_Code.TF_OK, s.Code);
var scalar = graph.OperationByName("scalar");
var feed = graph.OperationByName("feed");
var neg = graph.OperationByName("neg");
ASSERT_TRUE(scalar != IntPtr.Zero);
ASSERT_TRUE(feed != IntPtr.Zero);
ASSERT_TRUE(neg != IntPtr.Zero);
// Check return outputs
EXPECT_EQ(feed, return_outputs[0].oper);
EXPECT_EQ(0, return_outputs[0].index);
EXPECT_EQ(scalar, return_outputs[1].oper);
EXPECT_EQ(0, return_outputs[1].index);
opts.Dispose();
graph_def.Dispose();
graph.Dispose();
s.Dispose();
}
/// <summary>
/// `TEST(CAPI, ImportGraphDef_MissingUnusedInputMappings)`
/// </summary>
[TestMethod]
public void ImportGraphDef_MissingUnusedInputMappings()
{
}
[Ignore]
[TestMethod]
public void ImportGraphMeta()
{
var dir = "my-save-dir/";
using (var sess = tf.Session())
{
var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta");
new_saver.restore(sess, dir + "my-model-10000");
var labels = tf.constant(0, dtype: tf.int32, shape: new int[] { 100 }, name: "labels");
var batch_size = tf.size(labels);
var logits = tf.get_collection<ITensorOrOperation>("logits")[0] as Tensor;
var loss = tf.losses.sparse_softmax_cross_entropy(labels: labels,
logits: logits);
}
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

TensorFlow.NET 为 TensorFlow 提供 .NET 标准绑定
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/pepure/TensorFlow.NET.git
git@gitee.com:pepure/TensorFlow.NET.git
pepure
TensorFlow.NET
TensorFlow.NET
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /