A = [3 2; -1 0]; x = rand(2, 1); We want to compute the largest eigenvalue for A. repeat x = y / y.norm(); y = A * x; end
| 48 | // We want to compute the largest eigenvalue for A. |
| 49 | // repeat x = y / y.norm(); y = A * x; end |
| 50 | GraphDef CreateGraphDef() { |
| 51 | // TODO(jeff,opensource): This should really be a more interesting |
| 52 | // computation. Maybe turn this into an mnist model instead? |
| 53 | Scope root = Scope::NewRootScope(); |
| 54 | using namespace ::tensorflow::ops; // NOLINT(build/namespaces) |
| 55 | |
| 56 | // A = [3 2; -1 0]. Using Const<float> means the result will be a |
| 57 | // float tensor even though the initializer has integers. |
| 58 | auto a = Const<float>(root, {{3, 2}, {-1, 0}}); |
| 59 | |
| 60 | // x = [1.0; 1.0] |
| 61 | auto x = Const(root.WithOpName("x"), {{1.f}, {1.f}}); |
| 62 | |
| 63 | // y = A * x |
| 64 | auto y = MatMul(root.WithOpName("y"), a, x); |
| 65 | |
| 66 | // y2 = y.^2 |
| 67 | auto y2 = Square(root, y); |
| 68 | |
| 69 | // y2_sum = sum(y2). Note that you can pass constants directly as |
| 70 | // inputs. Sum() will automatically create a Const node to hold the |
| 71 | // 0 value. |
| 72 | auto y2_sum = Sum(root, y2, 0); |
| 73 | |
| 74 | // y_norm = sqrt(y2_sum) |
| 75 | auto y_norm = Sqrt(root, y2_sum); |
| 76 | |
| 77 | // y_normalized = y ./ y_norm |
| 78 | Div(root.WithOpName("y_normalized"), y, y_norm); |
| 79 | |
| 80 | GraphDef def; |
| 81 | TF_CHECK_OK(root.ToGraphDef(&def)); |
| 82 | |
| 83 | return def; |
| 84 | } |
| 85 | |
| 86 | string DebugString(const Tensor& x, const Tensor& y) { |
| 87 | CHECK_EQ(x.NumElements(), 2); |
no test coverage detected