diff --git a/README.md b/README.md index ee89da4c..421242cd 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp). ### Why TensorFlow.NET ? -`SciSharp STASK`'s mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Scince the APIs are kept as similar as possible you can immediately adapt any existing Tensorflow code in C# with a zero learning curve. Take a look at a comparison picture and see how comfortably a Tensorflow/Python script translates into a C# program with TensorFlow.NET. +`SciSharp STACK`'s mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Scince the APIs are kept as similar as possible you can immediately adapt any existing Tensorflow code in C# with a zero learning curve. Take a look at a comparison picture and see how comfortably a Tensorflow/Python script translates into a C# program with TensorFlow.NET. ![pythn vs csharp](docs/assets/syntax-comparision.png) @@ -131,7 +131,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow Run specific example in shell: ```cs -dotnet TensorFlowNET.Examples.dll "Retrain Image Classifier" +dotnet TensorFlowNET.Examples.dll "EXAMPLE NAME" ``` Example runner will download all the required files like training data and model pb files. @@ -142,13 +142,13 @@ Example runner will download all the required files like training data and model * [Logistic Regression](test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs) * [Nearest Neighbor](test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs) * [Naive Bayes Classification](test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs) +* [Full Connected Neural Network](test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs) * [Image Recognition](test/TensorFlowNET.Examples/ImageProcess) * [K-means Clustering](test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs) * [NN XOR](test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs) * [Object Detection](test/TensorFlowNET.Examples/ImageProcess/ObjectDetection.cs) * [Text Classification](test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs) * [CNN Text Classification](test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs) - * [Named Entity Recognition](test/TensorFlowNET.Examples/TextProcess/NER) * [Transfer Learning for Image Classification in InceptionV3](test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs) @@ -186,7 +186,9 @@ git pull upstream master Feel free to star or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET). -Join our chat on [Gitter](https://gitter.im/sci-sharp/community) or +Follow us on [Medium](https://medium.com/scisharp). + +Join our chat on [Gitter](https://gitter.im/sci-sharp/community). Scan QR code to join Tencent TIM group: diff --git a/TensorFlow.NET.sln b/TensorFlow.NET.sln index 523a148c..874f8082 100644 --- a/TensorFlow.NET.sln +++ b/TensorFlow.NET.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.168 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29025.244 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.UnitTest", "test\TensorFlowNET.UnitTest\TensorFlowNET.UnitTest.csproj", "{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}" EndProject diff --git a/docs/assets/TensorBoard-nn.png b/docs/assets/TensorBoard-nn.png new file mode 100644 index 00000000..23ccc3db Binary files /dev/null and b/docs/assets/TensorBoard-nn.png differ diff --git a/docs/assets/mnist.png b/docs/assets/mnist.png new file mode 100644 index 00000000..82481872 Binary files /dev/null and b/docs/assets/mnist.png differ diff --git a/docs/assets/nn-result.png b/docs/assets/nn-result.png new file mode 100644 index 00000000..7957b821 Binary files /dev/null and b/docs/assets/nn-result.png differ diff --git a/docs/assets/nn.png b/docs/assets/nn.png new file mode 100644 index 00000000..8fbb6f9b Binary files /dev/null and b/docs/assets/nn.png differ diff --git a/docs/source/Constant.md b/docs/source/Constant.md index 655a4b08..4d782f11 100644 --- a/docs/source/Constant.md +++ b/docs/source/Constant.md @@ -7,21 +7,12 @@ In TensorFlow, a constant is a special Tensor that cannot be modified while the * shape: dimensions; * name: constant's name; -在TensorFlow中,常量是一种特殊的Tensor,它在计算图运行的时候,不能被修改。比如在线性模型里$\tilde{y_i}=\boldsymbol{w}x_i+b$, 常数$b$就可以用一个常量来表示。既然常量是一种Tensor,那么它也就具有Tensor的所有数据特性,它包括: - -* value: 符合TensorFlow中定义的数据类型的常数值或者常数列表; -* dtype:数据类型; -* shape:常量的形状; -* name:常量的名字; - ##### How to create a Constant TensorFlow provides a handy function to create a Constant. In TF.NET, you can use the same function name `tf.constant` to create it. TF.NET takes the same name as python binding to the API. Naming, although this will make developers who are used to C# naming habits feel uncomfortable, but after careful consideration, I decided to give up the C# convention naming method. -TensorFlow提供了一个很方便的函数用来创建一个Constant, 在TF.NET,可以使用同样的函数名`tf.constant`来创建,TF.NET采取尽可能使用和python binding一样的命名方式来对API命名,虽然这样会让习惯C#命名习惯的开发者感到不舒服,但我经过深思熟虑之后还是决定放弃C#的约定命名方式。 - Initialize a scalar constant: ```csharp @@ -45,9 +36,7 @@ var tensor = tf.constant(nd); ##### Dive in Constant -Now let's explore how constant works. - -现在让我探究一下`tf.constant`是怎么工作的。 +Now let's explore how `constant` works. diff --git a/docs/source/Foreword.md b/docs/source/Foreword.md index af8ff8d3..256094f5 100644 --- a/docs/source/Foreword.md +++ b/docs/source/Foreword.md @@ -2,16 +2,10 @@ One of the most nerve-wracking periods when releasing the first version of an open source project occurs when the [gitter](https://gitter.im/sci-sharp/community) community is created. You are all alone, eagerly hoping and wishing for the first user to come along. I still vividly remember those days. -最让人紧张的时刻是当我为自己的开源项目发布第一个版本并在gitter里开放一个聊天社区,而里面只有你一个人,饥渴地等待第一个进入聊天室的用户,我仍然清楚地记得那个时期。 - TensorFlow.NET is my third open source project. BotSharp and NumSharp are the first two. The response is pretty good. I also got a lot of stars on github. Although the first two projects are very difficult, I can't admit that TensorFlow.NET is much more difficult than the previous two, and it is an area I have never been involved with. Mainly related to GPU parallel computing, distributed computing and neural network model. When I started writing this project, I was also sorting out the idea of the coding process. TensorFlow is a huge and complicated project, and it is easy to go beyond the scope of personal ability. Therefore, I want to record the thoughts at the time as much as possible. The process of recording and sorting clears the way of thinking. -TensorFlow.NET是我写的第3个开源项目,BotSharp和NumSharp是前两个,反应都还不错,在github上也收获了不少星。虽然前两个项目的难度很大,但是我不得承认TensorFlow.NET的难度要比之前两个要大的多,是我从未涉入过的领域。主要涉及GPU并行计算,分布式计算和神经网络模型。当我开始写这个项目的时候,我同时也在整理编码过程时候的想法,TensorFlow是个巨大最复杂的工程,很容易超出个人能力范围,所以想尽可能地把当时的思路记录下来,也想趁着记录整理的过程把思路理清。 - All the examples in this book can be found in the github repository of TensorFlow.NET. When the source code and the code in the book are inconsistent, please refer to the source code. The sample code is typically located in the Example or UnitTest project. - -本书中的所有例子都可以在TensorFlow.NET的github仓库中找到,当源代码和书中的代码不一致时,请以源代码为准。示例代码一般都位于Example或者是UnitTest项目里。 \ No newline at end of file diff --git a/docs/source/HelloWorld.md b/docs/source/HelloWorld.md index 5375ec15..7603eabd 100644 --- a/docs/source/HelloWorld.md +++ b/docs/source/HelloWorld.md @@ -2,11 +2,9 @@ I would describe TensorFlow as an open source machine learning framework developed by Google which can be used to build neural networks and perform a variety of machine learning tasks. it works on data flow graph where nodes are the mathematical operations and the edges are the data in the form of tensor, hence the name Tensor-Flow. -按照我的理解,TensorFlow是Google公司开发的一个开源机器学习框架,可以用来搭建神经网络模型和其它传统机器学习模型,它采用了图计算模型,图的节点和边分别代表了操作和数据输入或输出,数据在图的单个方向传递,因此这个过程形象地取名叫做TensorFlow。 -Let's run a classic HelloWorld program first and see if TensorFlow is running on .NET. I can't think of a simpler way to be a HelloWorld. -让我们先运行一个经典的HelloWorld程序,看看TensorFlow在.NET上面运行的效果,我想不出有比做个HelloWorld更简单的方式了。 +Let's run a classic HelloWorld program first and see if TensorFlow is running on .NET. I can't think of a simpler way to be a HelloWorld. @@ -14,7 +12,7 @@ Let's run a classic HelloWorld program first and see if TensorFlow is running on TensorFlow.NET uses the .NET Standard 2.0 standard, so your new project Target Framework can be .NET Framework or .NET Core. All the examples in this book are using .NET Core 2.2 and Microsoft Visual Studio Community 2017. To start building TensorFlow program you just need to download and install the .NET SDK (Software Development Kit). You have to download the latest .NET Core SDK from offical website: https://dotnet.microsoft.com/download. -TensorFlow.NET采用.NET标准库2.0版本,因此你的新建工程可以是.NET Framework或者是基于.NET Core的。本文中的所有例子都是用的.NET Core 2.2的,IDE用的是Microsoft Visual Studio Community 2017。为了能编译和运行TensorFlow工程,你需要从这里下载最新的.NET Core SDK: https://dotnet.microsoft.com/download。 + 1. New a project @@ -34,7 +32,7 @@ PM> Install-Package TensorFlow.NET After installing the TensorFlow.NET package, you can use the `using Tensorflow` to introduce the TensorFlow library. -安装完TensorFlow.NET包后,你就可以使用`using Tensorflow`来引入TensorFlow库了。 + ```csharp using System; @@ -76,5 +74,3 @@ Press any key to continue . . . This sample code can be found at [here](https://github.com/SciSharp/TensorFlow.NET/blob/master/test/TensorFlowNET.Examples/HelloWorld.cs). -此示例代码可以在[这里](https://github.com/SciSharp/TensorFlow.NET/blob/master/test/TensorFlowNET.Examples/HelloWorld.cs)找到。 - diff --git a/docs/source/LinearRegression.md b/docs/source/LinearRegression.md index d9921270..81a6dbc4 100644 --- a/docs/source/LinearRegression.md +++ b/docs/source/LinearRegression.md @@ -1,5 +1,7 @@ # Chapter. Linear Regression + + ### What is linear regression? Linear regression is a linear approach to modelling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). @@ -8,9 +10,7 @@ Consider the case of a single variable of interest y and a single predictor vari We have some data $D=\{x{\tiny i},y{\tiny i}\}$ and we assume a simple linear model of this dataset with Gaussian noise: -线性回归是一种线性建模方法,这种方法用来描述自变量与一个或多个因变量的之间的关系。在只有一个因变量y和一个自变量的情况下。自变量还有以下几种叫法: -协变量,输入,特征;因变量通常被叫做响应变量,输出,输出结果。 -假如我们有数据$D=\{x{\tiny i},y{\tiny i}\}$,并且假设这个数据集是满足高斯分布的线性模型: + ```csharp // Prepare training Data var train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f, 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f); @@ -21,15 +21,13 @@ var n_samples = train_X.shape[0]; Based on the given data points, we try to plot a line that models the points the best. The red line can be modelled based on the linear equation: $y = wx + b$. The motive of the linear regression algorithm is to find the best values for $w$ and $b$. Before moving on to the algorithm, le's have a look at two important concepts you must know to better understand linear regression. -按照上图根据数据描述的数据点,在这些数据点之间画出一条线,这条线能达到最好模拟点的分布的效果。红色的线能够通过下面呢线性等式来描述:$y = wx + b$。 -线性回归算法的目标就是找到这条线对应的最好的参数$w$和$b$。在介绍线性回归算法之前,我们先看两个重要的概念,这两个概念有助于你理解线性回归算法。 + ### Cost Function The cost function helps us to figure out the best possible values for $w$ and $b$ which would provide the best fit line for the data points. Since we want the best values for $w$ and $b$, we convert this search problem into a minimization problem where we would like to minimize the error between the predicted value and the actual value. -损失函数帮助我们估算出最优的参数$w$和$b$,这个最优的参数能够最好的拟合数据点的分布。由于我们想找到最优的参数$w$和$b$,因此我们把这个问题转化成求 -预测参数与实际参数之差的最小值问题。 + ![minimize-square-cost](_static/minimize-square-cost.png) @@ -37,8 +35,7 @@ We choose the above function to minimize. The difference between the predicted v value by the total number of data points. This provides the average squared error over all the data points. Therefore, this cost function is also known as the Mean Squared Error(MSE) function. Now, using this MSE function we are going to change the values of $w$ and $b$ such that the MSE value settles at the minima. -我们选择最小化上面的函数。预测值和真实值之间的差异的大小衡量了预测结果的偏差。我们用所有点的偏差的平方和除以所有点所有点的数量大小来表示说有点的平均 -的误差大小。因此,损失函数又叫均方误差(简称MSE)。到此,我们可以通过调整参数$w$和$b$来使MSE达到最小值。 + ```csharp // tf Graph Input @@ -56,13 +53,13 @@ var pred = tf.add(tf.multiply(X, W), b); var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples); ``` + + ### Gradient Descent -### 梯度下降法 The another important concept needed to understand is gradient descent. Gradient descent is a method of updating $w$ and $b$ to minimize the cost function. The idea is that we start with some random values for $w$ and $b$ and then we change these values iteratively to reduce the cost. Gradient descent helps us on how to update the values or which direction we would go next. Gradient descent is also know as **steepest descent**. -另一个需要理解的重要概念是梯度下降法。梯度下降法是通过更新参数$w$和$b$来最小化损失函数。梯度下降法的思想就是首先以任意的参数$w$和$b$开始计算损失 -函数,然后通过递归的方式不断地变化参数来减小损失。梯度下降法帮助我们如何更新参数,或者说告诉我们下一个参数该如何设置。梯度下降法也称为“最快下降法”。 + ![gradient-descent](_static/gradient-descent.png) @@ -72,9 +69,7 @@ of steps to reach the bottom. If you decide to take one step at a time you would reach sooner but, there is a chance that you could overshoot the bottom of the pit and not exactly at the bottom. In the gradient descent algorithm, the number of steps you take is the learning rate. This decides on how fast the algorithm converges to the minima. -这里做一个类比,想象着你站在一个U形坑的最上面,你的目标是达到坑的最低端。有一个条件是,你不确定你走多少步能到达底端。如果你选择一步一步的走到坑的 -底端,这样可能需要的时间很长。如果你每次大步的往前走,你可能很快到达坑的底端,但是你有可能错过坑的最底端。在梯度下降算法中,你所采用的步数就是训练 -速率。训练速率决定了算法以多块的速度使得损失函数达到最小值。 + ```csharp diff --git a/docs/source/LogisticRegression.md b/docs/source/LogisticRegression.md index ba0b8f57..42cda898 100644 --- a/docs/source/LogisticRegression.md +++ b/docs/source/LogisticRegression.md @@ -4,11 +4,11 @@ Logistic regression is a statistical analysis method used to predict a data value based on prior observations of a data set. A logistic regression model predicts a dependent data variable by analyzing the relationship between one or more existing independent variables. -逻辑回归是一种统计分析方法,用于根据已有得观察数据来预测未知数据。逻辑回归模型通过分析一个或多个现有自变量之间的关系来预测从属数据变量。 + The dependent variable of logistics regression can be two-category or multi-category, but the two-category is more common and easier to explain. So the most common use in practice is the logistics of the two classifications. An example used by TensorFlow.NET is a hand-written digit recognition, which is a multi-category. -逻辑回归的因变量可以是二分类的,也可以是多分类的,但是二分类的更为常用,也更加容易解释。 TensorFlow.NET用的例子是一个手写数字识别,它是一个多分类的问题。 + Softmax regression allows us to handle ![1557035393445](_static\logistic-regression\1557035393445.png) where K is the number of classes. diff --git a/docs/source/NeuralNetwork.md b/docs/source/NeuralNetwork.md new file mode 100644 index 00000000..1d46111b --- /dev/null +++ b/docs/source/NeuralNetwork.md @@ -0,0 +1,244 @@ +# Chapter. Neural Network + +In this chapter, we'll learn how to build a graph of neural network model. The key advantage of neural network compared to Linear Classifier is that it can separate data which it not linearly separable. We'll implement this model to classify hand-written digits images from the MNIST dataset. + + + +The structure of the neural network we're going to build is as follows. The hand-written digits images of the MNIST data which has 10 classes (from 0 to 9). The network is with 2 hidden layers: the first layer with 200 hidden units (neurons) and the second one (known as classifier layer) with 10 neurons. + +![neural network architecture](../assets/nn.png) + +Get started with the implementation step by step: + +1. **Prepare data** + + MNIST is dataset of handwritten digits which contains 55,000 examples for training, 5,000 examples for validation and 10,000 example for testing. The digits have been size-normalized and centered in a fixed-size image (28 x 28 pixels) with values from 0 and 1.Each image has been flattened and converted to a 1-D array of 784 features. It's also kind of benchmark of datasets for deep learning. + + ![MNIST dataset](../assets/mnist.png) + + We define some variables makes it easier to modify them later. It's important to note that in a linear model, we have to flatten the input images to a vector. + + ```csharp + using System; + using NumSharp; + using Tensorflow; + using TensorFlowNET.Examples.Utility; + using static Tensorflow.Python; + ``` + + ```csharp + const int img_h = 28; + const int img_w = 28; + int img_size_flat = img_h * img_w; // 784, the total number of pixels + int n_classes = 10; // Number of classes, one class per digit + ``` + + We'll write the function which automatically loads the MNIST data and returns it in our desired shape and format. There is an MNIST data helper to make life easier. + + ```csharp + Datasets mnist; + public void PrepareData() + { + mnist = MnistDataSet.read_data_sets("mnist", one_hot: true); + } + ``` + + Other than a function for loading the images and corresponding labels, we still need two more functions: + + **randomize**: which randomizes the order of images and their labels. At the beginning of each epoch, we will re-randomize the order of data samples to make sure that the trained model is not sensitive to the order of data. + + ```csharp + private (NDArray, NDArray) randomize(NDArray x, NDArray y) + { + var perm = np.random.permutation(y.shape[0]); + + np.random.shuffle(perm); + return (mnist.train.images[perm], mnist.train.labels[perm]); + } + ``` + + **get_next_batch**: which only selects a few number of images determined by the batch_size variable (as per Stochastic Gradient Descent method). + + ```csharp + private (NDArray, NDArray) get_next_batch(NDArray x, NDArray y, int start, int end) + { + var x_batch = x[$"{start}:{end}"]; + var y_batch = y[$"{start}:{end}"]; + return (x_batch, y_batch); + } + ``` + +2. **Set Hyperparameters** + + There're about 55,000 images in training set, it takes a long time to calculate the gradient of the model using all there images. Therefore we use a small batch of images in each iteration of the optimizer by Stochastic Gradient Descent. + + * epoch: one forward pass and one backward pass of all the training examples. + * batch size: the number of training examples in one forward/backward pass. The higher the batch size, the more memory space you'll need. + * iteration: one forward pass and one backward pass of one batch of images the training examples. + + ```csharp + int epochs = 10; + int batch_size = 100; + float learning_rate = 0.001f; + int h1 = 200; // number of nodes in the 1st hidden layer + ``` + +3. **Building the neural network** + + Let's make some functions to help build computation graph. + + **variables**: We need to define two variables `W` and `b` to construct our linear model. We use `Tensorflow Variables` of proper size and initialization to define them. + + ```csharp + // weight_variable + var in_dim = x.shape[1]; + + var initer = tf.truncated_normal_initializer(stddev: 0.01f); + var W = tf.get_variable("W_" + name, + dtype: tf.float32, + shape: (in_dim, num_units), + initializer: initer); + + // bias_variable + var initial = tf.constant(0f, num_units); + var b = tf.get_variable("b_" + name, + dtype: tf.float32, + initializer: initial); + ``` + + **fully-connected layer**: Neural network consists of stacks of fully-connected (dense) layers. Having the weight (W) and bias (b) variables, a fully-connected layer is defined as `activation(W x X + b)`. The complete `fc_layer` function is as below: + + ```csharp + private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = true) + { + var in_dim = x.shape[1]; + + var initer = tf.truncated_normal_initializer(stddev: 0.01f); + var W = tf.get_variable("W_" + name, + dtype: tf.float32, + shape: (in_dim, num_units), + initializer: initer); + + var initial = tf.constant(0f, num_units); + var b = tf.get_variable("b_" + name, + dtype: tf.float32, + initializer: initial); + + var layer = tf.matmul(x, W) + b; + if (use_relu) + layer = tf.nn.relu(layer); + + return layer; + } + ``` + + **inputs**: Now we need to define the proper tensors to feed in the input to our model. Placeholder variable is the suitable choice for the input images and corresponding labels. This allow us to change the inputs (images and labels) to the TensorFlow graph. + + ```csharp + // Placeholders for inputs (x) and outputs(y) + x = tf.placeholder(tf.float32, shape: (-1, img_size_flat), name: "X"); + y = tf.placeholder(tf.float32, shape: (-1, n_classes), name: "Y"); + ``` + + Placeholder `x` is defined for the images, the shape is set to `[None, img_size_flat]`, where `None` means that the tensor may hold an arbitrary number of images with each image being a vector of length `img_size_flat`. + + Placeholder `y` is the variable for the true labels associated with the images that were input in the placeholder variable `x`. It holds an arbitrary number of labels and each label is a vector of length `num_classes` which is 10. + + **network layers**: After creating the proper input, we have to pass it to our model. Since we have a neural network, we can stack multiple fully-connected layers using `fc_layer` method. Note that we will not use any activation function (use_relu = false) in the last layer. The reason is that we can use `tf.nn.softmax_cross_entropy_with_logits` to calculate the loss. + + ```csharp + // Create a fully-connected layer with h1 nodes as hidden layer + var fc1 = fc_layer(x, h1, "FC1", use_relu: true); + // Create a fully-connected layer with n_classes nodes as output layer + var output_logits = fc_layer(fc1, n_classes, "OUT", use_relu: false); + ``` + + **loss function**: After creating the network, we have to calculate the loss and optimize it, we have to calculate the `correct_prediction` and `accuracy`. + + ```csharp + // Define the loss function, optimizer, and accuracy + var logits = tf.nn.softmax_cross_entropy_with_logits(labels: y, logits: output_logits); + loss = tf.reduce_mean(logits, name: "loss"); + optimizer = tf.train.AdamOptimizer(learning_rate: learning_rate, name: "Adam-op").minimize(loss); + var correct_prediction = tf.equal(tf.argmax(output_logits, 1), tf.argmax(y, 1), name: "correct_pred"); + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name: "accuracy"); + ``` + + **initialize variables**: We have to invoke a variable initializer operation to initialize all variables. + + ```csharp + var init = tf.global_variables_initializer(); + ``` + + The complete computation graph is looks like below: + + ![TensorBoard-nn](../assets/TensorBoard-nn.png) + +4. **Train** + + After creating the graph, we can train our model. To train the model, we have to create a session and run the graph in the session. + + ```csharp + // Number of training iterations in each epoch + var num_tr_iter = mnist.train.labels.len / batch_size; + with(tf.Session(), sess => + { + sess.run(init); + + float loss_val = 100.0f; + float accuracy_val = 0f; + + foreach (var epoch in range(epochs)) + { + print($"Training epoch: {epoch + 1}"); + // Randomly shuffle the training data at the beginning of each epoch + var (x_train, y_train) = randomize(mnist.train.images, mnist.train.labels); + + foreach (var iteration in range(num_tr_iter)) + { + var start = iteration * batch_size; + var end = (iteration + 1) * batch_size; + var (x_batch, y_batch) = get_next_batch(x_train, y_train, start, end); + + // Run optimization op (backprop) + sess.run(optimizer, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + + if (iteration % display_freq == 0) + { + // Calculate and display the batch loss and accuracy + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + loss_val = result[0]; + accuracy_val = result[1]; + print($"iter {iteration.ToString("000")}: Loss={loss_val.ToString("0.0000")}, Training Accuracy={accuracy_val.ToString("P")}"); + } + } + + // Run validation after every epoch + var results1 = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.validation.images), new FeedItem(y, mnist.validation.labels)); + loss_val = results1[0]; + accuracy_val = results1[1]; + print("---------------------------------------------------------"); + print($"Epoch: {epoch + 1}, validation loss: {loss_val.ToString("0.0000")}, validation accuracy: {accuracy_val.ToString("P")}"); + print("---------------------------------------------------------"); + } + }); + ``` + +5. **Test** + + After the training is done, we have to test our model to see how good it performs on a new dataset. + + ```csharp + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.test.images), new FeedItem(y, mnist.test.labels)); + loss_test = result[0]; + accuracy_test = result[1]; + print("---------------------------------------------------------"); + print($"Test loss: {loss_test.ToString("0.0000")}, test accuracy: {accuracy_test.ToString("P")}"); + print("---------------------------------------------------------"); + ``` + + ![result](../assets/nn-result.png) + + + + diff --git a/docs/source/Placeholder.md b/docs/source/Placeholder.md index 2407bfca..a578a127 100644 --- a/docs/source/Placeholder.md +++ b/docs/source/Placeholder.md @@ -2,7 +2,7 @@ In this chapter we will talk about another common data type in TensorFlow: Placeholder. It is a simplified variable that can be passed to the required value by the session when the graph is run, that is, when you build the graph, you don't need to specify the value of that variable, but delay the session to the beginning. In TensorFlow terminology, we then feed data into the graph through these placeholders. The difference between placeholders and constants is that placeholders can specify coefficient values more flexibly without modifying the code that builds the graph. For example, mathematical constants are suitable for Constant, and some model smoothing values can be specified with Placeholder. -这章我们讲一下TensorFlow里的另一种常用数据类型:占位符。它是一种简化的变量,可以在图运行的时候由会话传入所需要的值,就是说你在构建图的时候,不需要具体指定那个变量的值,而是延迟到会话开始的时候以参数的方式从外部传入初始值。占位符和常量的区别是占位符可以更灵活的指定系数值,而不需要修改构建图的代码。比如数学常量就适合用Constant, 有些模型平滑值可以用Placeholder来指定。 + ```csharp var x = tf.placeholder(tf.int32); diff --git a/docs/source/Preface.md b/docs/source/Preface.md index c924c280..65db8c1d 100644 --- a/docs/source/Preface.md +++ b/docs/source/Preface.md @@ -6,36 +6,10 @@ Why do I start the TensorFlow.NET project? In a few days, it was Christmas in 2018. I watched my children grow up and be sensible every day, and I felt that time passed too fast. IT technology updates are faster than ever, and a variety of front-end technologies are emerging. Big data, Artificial Intelligence and Blockchain, Container technology and Microservices, Distributed Computing and Serverless technology are dazzling. The Amazon AI service interface claims that engineers who don't need any machine learning experience can use it, so that the idea of just calming down for two years and planning to switch to an AI architecture in the future is a splash of cold water. -再过几天就是2018年圣诞节,看着孩子一天天长大并懂事,感慨时间过得太快。IT技术更新换代比以往任何时候都更快,各种前后端技术纷纷涌现。大数据,人工智能和区块链,容器技术和微服务,分布式计算和无服务器技术,让人眼花缭乱。Amazon AI服务接口宣称不需要具有任何机器学习经验的工程师就能使用,让像我这样刚静下心来学习了两年并打算将来转行做AI架构的想法泼了一桶凉水。 - TensorFlow is an open source project for machine learning especially for deep learning. It's used for both research and production at Google company. It's designed according to dataflow programming pattern across a range of tasks. TensorFlow is not just a deep learning library. As long as you can represent your calculation process as a data flow diagram, you can use TensorFlow for distributed computing. TensorFlow uses a computational graph to build a computing network while operating on the graph. Users can write their own upper-level models in Python based on TensorFlow, or extend the underlying C++ custom action code to TensorFlow. -TensorFlow是一个用于机器学习的开源项目,尤其适用于深度学习。 它最初是谷歌公司的用于内部研究和生产的工具,后来开源出来给社区使用。TensorFlow并不仅仅是一个深度学习库,只要可以把你的计算过程表示称一个数据流图的过程,就可以使用TensorFlow来进行分布式计算。TensorFlow用计算图的方式建立计算网络,同时对图进行操作。用户可以基于TensorFlow的基础上用python编写自己的上层模型,也可以扩展底层的C++自定义操作代码添加到TensorFlow中。 - In order to avoid confusion, the unique classes defined in TensorFlow are not translated in this book. For example, Tensor, Graph, Shape will retain the English name. - -为了避免混淆,本书中对TensorFlow中定义的特有类不进行翻译,比如Tensor, Graph, Session, Shape这些词都会保留英文名称。 - - - -Terminology 术语: - -TF: Google TensorFlow - -TF.NET: TensorFlow.NET - -Graph: 计算图 - -Session: 会话 - -Variable: 变量 - -Tensor: 张量 - -Operation: 操作 - -Node: 节点 \ No newline at end of file diff --git a/docs/source/Session.md b/docs/source/Session.md index ffa5882e..d6f24904 100644 --- a/docs/source/Session.md +++ b/docs/source/Session.md @@ -2,13 +2,13 @@ TensorFlow **session** runs parts of the graph across a set of local and remote devices. A session allows to execute graphs or part of graphs. It allocates resources (on one or more machines) for that and holds the actual values of intermediate results and variables. -TensorFlow **Session** 运行预定义的计算图,并且支持跨设备运行和分配GPU。Session可以运行整个计算图或者图的一部分,这样做的好处是对开发模型来话很方便,不需要每次都执行整个图。会话还负责当前计算图的内存分配,保留和传递中间结果。 + ### Running Computations in a Session Let's complete the example in last chapter. To run any of the operations, we need to create a session for that graph. The session will also allocate memory to store the current value of the variable. -让我们完成上一章的例子,在那个例子里我们只是定义了一个图的结构。为了运行这个图,我们需要创建一个Session来根据图定义来分配资源运行它。 + ```csharp with(tf.Graph(), graph => @@ -27,5 +27,3 @@ with(tf.Graph(), graph => ``` The value of our variables is only valid within one session. If we try to get the value in another session. TensorFlow will raise an error of `Attempting to use uninitialized value foo`. Of course, we can use the graph in more than one session, because session copies graph definition to new memory area. We just have to initialize the variables again. The values in the new session will be completely independent from the previous one. - -变量值只会在一个Session里有效。如果我们试图从本Session来访问另一个Session创建的变量和值,就会得到一个`变量未初始化`的错误提示。当然,我们能从多个Session运行同一个计算图,因为计算图只是一个定义,Session初始化的时候会复制整图的定义到新的内存空间里。所以每个Session里的变量值是互相隔离的。 \ No newline at end of file diff --git a/docs/source/Tensor.md b/docs/source/Tensor.md index b460bede..50cc6a44 100644 --- a/docs/source/Tensor.md +++ b/docs/source/Tensor.md @@ -8,16 +8,12 @@ Tensor holds a multi-dimensional array of elements of a single data type which is very similar with numpy's ndarray. When the dimension is zero, it can be called a scalar. When the dimension is 2, it can be called a matrix. When the dimension is greater than 2, it is usually called a tensor. If you are very familiar with numpy, then understanding Tensor will be quite easy. -Tensor是一个具有单一数据类型的多维数组容器,当维度为零时,可以称之为标量,当维度为2时,可以称之为矩阵,当维度大于2时,通常称之为张量。Tensor的数据结构非常类似于numpy里的ndarray。如果你对numpy非常熟悉的话,那么对Tensor的理解会相当容易。 - ##### How to create a Tensor? There are many ways to initialize a Tensor object in TF.NET. It can be initialized from a scalar, string, matrix or tensor. -在TF.NET中有很多种方式可以初始化一个Tensor对象。它可以从一个标量,字符串,矩阵或张量来初始化。 - ```csharp // Create a tensor holds a scalar value var t1 = new Tensor(3); @@ -42,8 +38,6 @@ Console.WriteLine($"t1: {t1}, t2: {t2}, t3: {t3}"); TF uses column major order. If we use NumSharp to generate a 2 x 3 matrix, if we access the data from 0 to 5 in order, we won't get a number of 1-6, but we get the order of 1, 4, 2, 5, 3, 6. a set of numbers. -TF 采用的是按列存储模式,如果我们用NumSharp产生一个2 X 3的矩阵,如果按顺序从0到5访问数据的话,是不会得到1-6的数字的,而是得到1,4, 2, 5, 3, 6这个顺序的一组数字。 - ```cs // Generate a matrix:[[1, 2, 3], [4, 5, 6]] var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3); diff --git a/docs/source/Variable.md b/docs/source/Variable.md index 48eaa9ae..c4f6a6af 100644 --- a/docs/source/Variable.md +++ b/docs/source/Variable.md @@ -2,7 +2,7 @@ The variables in TensorFlow are mainly used to represent variable parameter values in the machine learning model. Variables can be initialized by the `tf.Variable` function. During the graph computation the variables are modified by other operations. Variables exist in the session, as long as they are in the same session, other computing nodes on the network can access the same variable value. Variables use lazy loading and will only request memory space when they are used. -TensorFlow中变量主要用来表示机器学习模型中的可变参数值,变量通过可以通过`tf.Variable` 类进行初始化。在图运行过程中,通过各种操作对变量进行修改。变量存在于会话当中,只要是在同一个会话里,网络上的其它计算结节都可以访问到相同的变量值。变量采用延迟加载的方式,只有使用的时候才会申请内存空间。 + ```csharp var x = tf.Variable(10, name: "x"); @@ -16,4 +16,3 @@ using (var session = tf.Session()) The above code first creates a variable operation, initializes the variable, then runs the session, and finally gets the result. This code is very simple, but it shows the complete process how TensorFlow operates on variables. When creating a variable, you pass a `tensor` as the initial value to the function `Variable()`. TensorFlow provides a series of operators to initialize the tensor, the initial value is a constant or a random value. -以上代码先创建变量操作,初始化变量,再运行会话,最后得到结果。这段代码非常简单,但是它体现了整个TensorFlow对变量操作的完整流程。当创建一个变量时,你将一个`张量`作为初始值传入函数`Variable()`。TensorFlow提供了一系列操作符来初始化张量,初始值是常量或是随机值。 \ No newline at end of file diff --git a/docs/source/index.rst b/docs/source/index.rst index e26f8378..4cddbd6a 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -28,4 +28,5 @@ Welcome to TensorFlow.NET's documentation! LinearRegression LogisticRegression NearestNeighbor - ImageRecognition \ No newline at end of file + ImageRecognition + NeuralNetwork \ No newline at end of file diff --git a/src/KerasNET.Core/Layers/Dense.cs b/src/KerasNET.Core/Layers/Dense.cs index c6c086a4..216de124 100644 --- a/src/KerasNET.Core/Layers/Dense.cs +++ b/src/KerasNET.Core/Layers/Dense.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using System.Linq; @@ -40,7 +56,7 @@ namespace Keras.Layers var dot = tf.matmul(x, W); if (this.activation != null) dot = activation.Activate(dot); - Console.WriteLine("Calling Layer \"" + name + "(" + np.array(dot.GetShape().Dimensions).ToString() + ")\" ..."); + Console.WriteLine("Calling Layer \"" + name + "(" + np.array(dot.TensorShape.Dimensions).ToString() + ")\" ..."); return dot; } public TensorShape __shape__() diff --git a/src/KerasNET.Core/Model.cs b/src/KerasNET.Core/Model.cs index cb960169..e35b67ee 100644 --- a/src/KerasNET.Core/Model.cs +++ b/src/KerasNET.Core/Model.cs @@ -1,4 +1,20 @@ -using Keras.Layers; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Keras.Layers; using NumSharp; using System; using System.Collections.Generic; @@ -65,7 +81,7 @@ namespace Keras #endregion #region Model Graph Form Layer Stack - var flow_shape = features.GetShape(); + var flow_shape = features.TensorShape; Flow = features; for (int i = 0; i < layer_stack.Count; i++) { diff --git a/src/TensorFlowNET.Core/APIs/c_api.cs b/src/TensorFlowNET.Core/APIs/c_api.cs index 93d93959..7b7ade95 100644 --- a/src/TensorFlowNET.Core/APIs/c_api.cs +++ b/src/TensorFlowNET.Core/APIs/c_api.cs @@ -1,5 +1,22 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; +using System.Net; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/keras.layers.cs b/src/TensorFlowNET.Core/APIs/keras.layers.cs index 00fe7ee1..3a2749f5 100644 --- a/src/TensorFlowNET.Core/APIs/keras.layers.cs +++ b/src/TensorFlowNET.Core/APIs/keras.layers.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/keras.preprocessing.cs b/src/TensorFlowNET.Core/APIs/keras.preprocessing.cs index 97363bfb..2be63216 100644 --- a/src/TensorFlowNET.Core/APIs/keras.preprocessing.cs +++ b/src/TensorFlowNET.Core/APIs/keras.preprocessing.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras; diff --git a/src/TensorFlowNET.Core/APIs/tf.array.cs b/src/TensorFlowNET.Core/APIs/tf.array.cs index 4e733f18..3dfdc3d3 100644 --- a/src/TensorFlowNET.Core/APIs/tf.array.cs +++ b/src/TensorFlowNET.Core/APIs/tf.array.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.control.cs b/src/TensorFlowNET.Core/APIs/tf.control.cs index 1b6bc3b8..abe0beb6 100644 --- a/src/TensorFlowNET.Core/APIs/tf.control.cs +++ b/src/TensorFlowNET.Core/APIs/tf.control.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.distributions.cs b/src/TensorFlowNET.Core/APIs/tf.distributions.cs index fec6626a..a1006309 100644 --- a/src/TensorFlowNET.Core/APIs/tf.distributions.cs +++ b/src/TensorFlowNET.Core/APIs/tf.distributions.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.exp.cs b/src/TensorFlowNET.Core/APIs/tf.exp.cs index 885cd96d..04f04148 100644 --- a/src/TensorFlowNET.Core/APIs/tf.exp.cs +++ b/src/TensorFlowNET.Core/APIs/tf.exp.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.gradients.cs b/src/TensorFlowNET.Core/APIs/tf.gradients.cs index 0961288c..6b6b2baf 100644 --- a/src/TensorFlowNET.Core/APIs/tf.gradients.cs +++ b/src/TensorFlowNET.Core/APIs/tf.gradients.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.graph.cs b/src/TensorFlowNET.Core/APIs/tf.graph.cs index c343050a..0496a5ee 100644 --- a/src/TensorFlowNET.Core/APIs/tf.graph.cs +++ b/src/TensorFlowNET.Core/APIs/tf.graph.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.init.cs b/src/TensorFlowNET.Core/APIs/tf.init.cs index c4cc415e..d053b72d 100644 --- a/src/TensorFlowNET.Core/APIs/tf.init.cs +++ b/src/TensorFlowNET.Core/APIs/tf.init.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations.Initializers; diff --git a/src/TensorFlowNET.Core/APIs/tf.io.cs b/src/TensorFlowNET.Core/APIs/tf.io.cs index f504bdc6..7f891d18 100644 --- a/src/TensorFlowNET.Core/APIs/tf.io.cs +++ b/src/TensorFlowNET.Core/APIs/tf.io.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.IO; diff --git a/src/TensorFlowNET.Core/APIs/tf.layers.cs b/src/TensorFlowNET.Core/APIs/tf.layers.cs index ea35e869..cd9a6cb1 100644 --- a/src/TensorFlowNET.Core/APIs/tf.layers.cs +++ b/src/TensorFlowNET.Core/APIs/tf.layers.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras.Layers; diff --git a/src/TensorFlowNET.Core/APIs/tf.linalg.cs b/src/TensorFlowNET.Core/APIs/tf.linalg.cs index fd751322..bbe8211c 100644 --- a/src/TensorFlowNET.Core/APIs/tf.linalg.cs +++ b/src/TensorFlowNET.Core/APIs/tf.linalg.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; @@ -11,5 +27,8 @@ namespace Tensorflow public static Tensor matmul(Tensor a, Tensor b) => gen_math_ops.mat_mul(a, b); + + public static Tensor batch_matmul(Tensor x, Tensor y) + => gen_math_ops.batch_mat_mul(x, y); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.loss.cs b/src/TensorFlowNET.Core/APIs/tf.loss.cs index 90162603..6561ae64 100644 --- a/src/TensorFlowNET.Core/APIs/tf.loss.cs +++ b/src/TensorFlowNET.Core/APIs/tf.loss.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.math.cs b/src/TensorFlowNET.Core/APIs/tf.math.cs index a8ec223a..0ce19616 100644 --- a/src/TensorFlowNET.Core/APIs/tf.math.cs +++ b/src/TensorFlowNET.Core/APIs/tf.math.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.nn.cs b/src/TensorFlowNET.Core/APIs/tf.nn.cs index bfa71f31..dae50e27 100644 --- a/src/TensorFlowNET.Core/APIs/tf.nn.cs +++ b/src/TensorFlowNET.Core/APIs/tf.nn.cs @@ -1,8 +1,25 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations; using Tensorflow.Operations.Activation; +using static Tensorflow.Python; namespace Tensorflow { @@ -10,6 +27,26 @@ namespace Tensorflow { public static class nn { + public static Tensor conv2d(Tensor input, RefVariable filter, int[] strides, string padding, bool use_cudnn_on_gpu = true, + string data_format= "NHWC", int[] dilations= null, string name = null) + { + var parameters = new Conv2dParams + { + Input = input, + Filter = filter, + Strides = strides, + Padding = padding, + UseCudnnOnGpu = use_cudnn_on_gpu, + DataFormat = data_format, + Name = name + }; + + if (dilations != null) + parameters.Dilations = dilations; + + return gen_nn_ops.conv2d(parameters); + } + /// /// Computes dropout. /// @@ -73,7 +110,10 @@ namespace Tensorflow is_training: is_training, name: name); - public static IPoolFunction max_pool => new MaxPoolFunction(); + public static IPoolFunction max_pool_fn => new MaxPoolFunction(); + + public static Tensor max_pool(Tensor value, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string name = null) + => nn_ops.max_pool(value, ksize, strides, padding, data_format: data_format, name: name); public static Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name = null) => gen_nn_ops.top_kv2(input, k: k, sorted: sorted, name: name); @@ -101,6 +141,25 @@ namespace Tensorflow Tensor logits = null, string name = null) => nn_ops.sparse_softmax_cross_entropy_with_logits(labels: labels, logits: logits, name: name); + /// + /// Computes softmax cross entropy between `logits` and `labels`. + /// + /// + /// + /// + /// + /// + public static Tensor softmax_cross_entropy_with_logits(Tensor labels, Tensor logits, int dim = -1, string name = null) + { + with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope => + { + name = scope; + labels = array_ops.stop_gradient(labels, name: "labels_stop_gradient"); + }); + + return softmax_cross_entropy_with_logits_v2(labels, logits, axis: dim, name: name); + } + public static Tensor softmax_cross_entropy_with_logits_v2(Tensor labels, Tensor logits, int axis = -1, string name = null) => nn_ops.softmax_cross_entropy_with_logits_v2_helper(labels, logits, axis: axis, name: name); } diff --git a/src/TensorFlowNET.Core/APIs/tf.ops.cs b/src/TensorFlowNET.Core/APIs/tf.ops.cs index f789d473..5d3e481c 100644 --- a/src/TensorFlowNET.Core/APIs/tf.ops.cs +++ b/src/TensorFlowNET.Core/APIs/tf.ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.random.cs b/src/TensorFlowNET.Core/APIs/tf.random.cs index b56df144..9e7408a5 100644 --- a/src/TensorFlowNET.Core/APIs/tf.random.cs +++ b/src/TensorFlowNET.Core/APIs/tf.random.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.reduce_logsumexp.cs b/src/TensorFlowNET.Core/APIs/tf.reduce_logsumexp.cs index 32140389..9905e64d 100644 --- a/src/TensorFlowNET.Core/APIs/tf.reduce_logsumexp.cs +++ b/src/TensorFlowNET.Core/APIs/tf.reduce_logsumexp.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.reshape.cs b/src/TensorFlowNET.Core/APIs/tf.reshape.cs index 30098581..eb8966f4 100644 --- a/src/TensorFlowNET.Core/APIs/tf.reshape.cs +++ b/src/TensorFlowNET.Core/APIs/tf.reshape.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.summary.cs b/src/TensorFlowNET.Core/APIs/tf.summary.cs index 618806dd..26a533da 100644 --- a/src/TensorFlowNET.Core/APIs/tf.summary.cs +++ b/src/TensorFlowNET.Core/APIs/tf.summary.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.IO; diff --git a/src/TensorFlowNET.Core/APIs/tf.tensor.cs b/src/TensorFlowNET.Core/APIs/tf.tensor.cs index 33d86e14..7acff3d5 100644 --- a/src/TensorFlowNET.Core/APIs/tf.tensor.cs +++ b/src/TensorFlowNET.Core/APIs/tf.tensor.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.tile.cs b/src/TensorFlowNET.Core/APIs/tf.tile.cs index 21a32a98..09fbc119 100644 --- a/src/TensorFlowNET.Core/APIs/tf.tile.cs +++ b/src/TensorFlowNET.Core/APIs/tf.tile.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/APIs/tf.variable.cs b/src/TensorFlowNET.Core/APIs/tf.variable.cs index a1b3e1d8..3e3a110d 100644 --- a/src/TensorFlowNET.Core/APIs/tf.variable.cs +++ b/src/TensorFlowNET.Core/APIs/tf.variable.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs index 8f067d3e..7db799dd 100644 --- a/src/TensorFlowNET.Core/Attributes/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Attributes/c_api.ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Buffers/Buffer.cs b/src/TensorFlowNET.Core/Buffers/Buffer.cs index 36b74e5c..3f949a62 100644 --- a/src/TensorFlowNET.Core/Buffers/Buffer.cs +++ b/src/TensorFlowNET.Core/Buffers/Buffer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Buffers/TF_Buffer.cs b/src/TensorFlowNET.Core/Buffers/TF_Buffer.cs index 3c3ac91e..71b27a58 100644 --- a/src/TensorFlowNET.Core/Buffers/TF_Buffer.cs +++ b/src/TensorFlowNET.Core/Buffers/TF_Buffer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Buffers/c_api.buffer.cs b/src/TensorFlowNET.Core/Buffers/c_api.buffer.cs index 08c71887..fddd2686 100644 --- a/src/TensorFlowNET.Core/Buffers/c_api.buffer.cs +++ b/src/TensorFlowNET.Core/Buffers/c_api.buffer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Clustering/KMeans.cs b/src/TensorFlowNET.Core/Clustering/KMeans.cs index 07c69a63..20cf30a6 100644 --- a/src/TensorFlowNET.Core/Clustering/KMeans.cs +++ b/src/TensorFlowNET.Core/Clustering/KMeans.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs index 0e437d51..6748cda8 100644 --- a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs +++ b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Contrib/Learn/Preprocessing/VocabularyProcessor.cs b/src/TensorFlowNET.Core/Contrib/Learn/Preprocessing/VocabularyProcessor.cs index 00f2c9b0..d4cc9ddc 100644 --- a/src/TensorFlowNET.Core/Contrib/Learn/Preprocessing/VocabularyProcessor.cs +++ b/src/TensorFlowNET.Core/Contrib/Learn/Preprocessing/VocabularyProcessor.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Framework/IndexedSlices.cs b/src/TensorFlowNET.Core/Framework/IndexedSlices.cs index 0c4f0c8b..721ef7ed 100644 --- a/src/TensorFlowNET.Core/Framework/IndexedSlices.cs +++ b/src/TensorFlowNET.Core/Framework/IndexedSlices.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Framework/c_api_util.cs b/src/TensorFlowNET.Core/Framework/c_api_util.cs new file mode 100644 index 00000000..300abf6e --- /dev/null +++ b/src/TensorFlowNET.Core/Framework/c_api_util.cs @@ -0,0 +1,136 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Net; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Tensorflow +{ + public class c_api_util + { + static bool isDllDownloaded = false; + static object locker = new object(); + public static void DownloadLibrary() + { + string dll = c_api.TensorFlowLibName; + string directory = AppDomain.CurrentDomain.BaseDirectory; + string file = ""; + string url = ""; + + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + dll = $"{dll}.dll"; + file = Path.Combine(directory, "libtensorflow-cpu-windows-x86_64-1.14.0.zip"); + url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.14.0.zip"; + break; + case PlatformID.Unix: + dll = $"lib{dll}.so"; + file = Path.Combine(directory, "libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz"); + url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz"; + break; + default: + throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}"); + } + + if (isDllDownloaded || File.Exists($"{directory}/{dll}")) + { + isDllDownloaded = true; + return; + } + + lock (locker) + { + if (!File.Exists(file)) + { + var wc = new WebClient(); + Console.WriteLine($"Downloading Tensorflow library from {url}..."); + var download = Task.Run(() => wc.DownloadFile(url, file)); + while (!download.IsCompleted) + { + Thread.Sleep(1000); + Console.Write("."); + } + Console.WriteLine(""); + Console.WriteLine($"Downloaded successfully."); + } + + Console.WriteLine($"Extracting..."); + var task = Task.Run(() => + { + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + ZipFile.ExtractToDirectory(file, directory); + Util.CmdHelper.Command($"move lib\\* .\\"); + Util.CmdHelper.Command($"rm -r lib"); + Util.CmdHelper.Command($"rm -r include"); + break; + case PlatformID.Unix: + Util.CmdHelper.Bash($"tar xvzf {file} ./lib/"); + Util.CmdHelper.Bash($"mv {directory}/lib/* {directory}"); + Util.CmdHelper.Bash($"rm -r {directory}/lib"); + break; + default: + throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}"); + } + }); + + while (!task.IsCompleted) + { + Thread.Sleep(100); + Console.Write("."); + } + + Console.WriteLine(""); + Console.WriteLine("Extraction is completed."); + } + + isDllDownloaded = true; + } + + public static TF_Output tf_output(IntPtr c_op, int index) => new TF_Output(c_op, index); + + public static ImportGraphDefOptions ScopedTFImportGraphDefOptions() => new ImportGraphDefOptions(); + + public static Buffer tf_buffer(byte[] data) => new Buffer(data); + + public static IEnumerable new_tf_operations(Graph graph) + { + foreach (var c_op in tf_operations(graph)) + { + if (graph._get_operation_by_tf_operation(c_op) == null) + yield return c_op; + } + } + + public static IEnumerable tf_operations(Graph graph) + { + uint pos = 0; + IntPtr c_op; + while ((c_op = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero) + { + yield return c_op; + } + } + } +} diff --git a/src/TensorFlowNET.Core/Framework/c_api_util.py.cs b/src/TensorFlowNET.Core/Framework/c_api_util.py.cs deleted file mode 100644 index 94c0dac0..00000000 --- a/src/TensorFlowNET.Core/Framework/c_api_util.py.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow -{ - public class c_api_util - { - public static TF_Output tf_output(IntPtr c_op, int index) => new TF_Output(c_op, index); - - public static ImportGraphDefOptions ScopedTFImportGraphDefOptions() => new ImportGraphDefOptions(); - - public static Buffer tf_buffer(byte[] data) => new Buffer(data); - - public static IEnumerable new_tf_operations(Graph graph) - { - foreach (var c_op in tf_operations(graph)) - { - if (graph._get_operation_by_tf_operation(c_op) == null) - yield return c_op; - } - } - - public static IEnumerable tf_operations(Graph graph) - { - uint pos = 0; - IntPtr c_op; - while ((c_op = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero) - { - yield return c_op; - } - } - } -} diff --git a/src/TensorFlowNET.Core/Framework/common_shapes.py.cs b/src/TensorFlowNET.Core/Framework/common_shapes.py.cs index e0a08184..249a7e14 100644 --- a/src/TensorFlowNET.Core/Framework/common_shapes.py.cs +++ b/src/TensorFlowNET.Core/Framework/common_shapes.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; @@ -37,7 +53,7 @@ namespace Tensorflow.Framework public static bool has_fully_defined_shape(Tensor tensor) { - return tensor.GetShape().is_fully_defined(); + return tensor.TensorShape.is_fully_defined(); } } } diff --git a/src/TensorFlowNET.Core/Framework/graph_util_impl.cs b/src/TensorFlowNET.Core/Framework/graph_util_impl.cs index 0ea334ea..7165b779 100644 --- a/src/TensorFlowNET.Core/Framework/graph_util_impl.cs +++ b/src/TensorFlowNET.Core/Framework/graph_util_impl.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Framework/importer.py.cs b/src/TensorFlowNET.Core/Framework/importer.py.cs index ee2ce305..c957ced5 100644 --- a/src/TensorFlowNET.Core/Framework/importer.py.cs +++ b/src/TensorFlowNET.Core/Framework/importer.py.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Framework/meta_graph.py.cs b/src/TensorFlowNET.Core/Framework/meta_graph.py.cs index 64a1f5d9..9e40bb46 100644 --- a/src/TensorFlowNET.Core/Framework/meta_graph.py.cs +++ b/src/TensorFlowNET.Core/Framework/meta_graph.py.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.IO; diff --git a/src/TensorFlowNET.Core/Framework/op_def_registry.py.cs b/src/TensorFlowNET.Core/Framework/op_def_registry.py.cs index 270b9056..8e2099cb 100644 --- a/src/TensorFlowNET.Core/Framework/op_def_registry.py.cs +++ b/src/TensorFlowNET.Core/Framework/op_def_registry.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.OpDef.Types; diff --git a/src/TensorFlowNET.Core/Framework/random_seed.py.cs b/src/TensorFlowNET.Core/Framework/random_seed.py.cs index df114b23..e48d86e5 100644 --- a/src/TensorFlowNET.Core/Framework/random_seed.py.cs +++ b/src/TensorFlowNET.Core/Framework/random_seed.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Framework/smart_module.cs b/src/TensorFlowNET.Core/Framework/smart_module.cs index 57c3f67f..b4dacfb5 100644 --- a/src/TensorFlowNET.Core/Framework/smart_module.cs +++ b/src/TensorFlowNET.Core/Framework/smart_module.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Functions/c_api.function.cs b/src/TensorFlowNET.Core/Functions/c_api.function.cs index 26a32470..68d07afc 100644 --- a/src/TensorFlowNET.Core/Functions/c_api.function.cs +++ b/src/TensorFlowNET.Core/Functions/c_api.function.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/RegisterGradient.cs b/src/TensorFlowNET.Core/Gradients/RegisterGradient.cs index f07c613d..0d0d5d7a 100644 --- a/src/TensorFlowNET.Core/Gradients/RegisterGradient.cs +++ b/src/TensorFlowNET.Core/Gradients/RegisterGradient.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/array_grad.cs b/src/TensorFlowNET.Core/Gradients/array_grad.cs index eec3521b..2708bd79 100644 --- a/src/TensorFlowNET.Core/Gradients/array_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/array_grad.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs b/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs index 9765743a..6b4b1d64 100644 --- a/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs +++ b/src/TensorFlowNET.Core/Gradients/c_api.gradient.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/control_flow_grad.py.cs b/src/TensorFlowNET.Core/Gradients/control_flow_grad.py.cs index ec2a16a4..fcae93b2 100644 --- a/src/TensorFlowNET.Core/Gradients/control_flow_grad.py.cs +++ b/src/TensorFlowNET.Core/Gradients/control_flow_grad.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs index 8ad4b44e..b0a1d15c 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/gradients_util.cs b/src/TensorFlowNET.Core/Gradients/gradients_util.cs index a2a388ce..6b96a21c 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_util.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_util.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/math_grad.cs b/src/TensorFlowNET.Core/Gradients/math_grad.cs index f7f8e35f..9ab64848 100644 --- a/src/TensorFlowNET.Core/Gradients/math_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/math_grad.cs @@ -1,4 +1,19 @@ -//using Newtonsoft.Json; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using System; using System.Collections.Generic; using System.Linq; @@ -153,6 +168,11 @@ namespace Tensorflow.Gradients return new Tensor[] { grad_a, grad_b }; } + public static Tensor[] _BatchMatMul(Operation op, Tensor[] grads) + { + throw new NotImplementedException(); + } + [RegisterGradient("Mean")] public static Tensor[] _MeanGrad(Operation op, Tensor[] grads) { diff --git a/src/TensorFlowNET.Core/Gradients/nn_grad.cs b/src/TensorFlowNET.Core/Gradients/nn_grad.cs index 71a833ad..7df22722 100644 --- a/src/TensorFlowNET.Core/Gradients/nn_grad.cs +++ b/src/TensorFlowNET.Core/Gradients/nn_grad.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs b/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs index 477a39ff..ac6cea74 100644 --- a/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs +++ b/src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; diff --git a/src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs b/src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs index fa9e0312..5a44c808 100644 --- a/src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs +++ b/src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/FreezeGraph.cs b/src/TensorFlowNET.Core/Graphs/FreezeGraph.cs index a77b4f3a..20556215 100644 --- a/src/TensorFlowNET.Core/Graphs/FreezeGraph.cs +++ b/src/TensorFlowNET.Core/Graphs/FreezeGraph.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Control.cs b/src/TensorFlowNET.Core/Graphs/Graph.Control.cs index fda9ff01..d0e547fa 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Control.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Control.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Export.cs b/src/TensorFlowNET.Core/Graphs/Graph.Export.cs index 0ca80be3..dcde9677 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Export.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Export.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Import.cs b/src/TensorFlowNET.Core/Graphs/Graph.Import.cs index 776246b1..776d115e 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Import.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Import.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.IO; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs b/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs index 22703fbc..c3bd44b9 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.Operation.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; diff --git a/src/TensorFlowNET.Core/Graphs/Graph.cs b/src/TensorFlowNET.Core/Graphs/Graph.cs index 66bd6bbe..ab4bdb0a 100644 --- a/src/TensorFlowNET.Core/Graphs/Graph.cs +++ b/src/TensorFlowNET.Core/Graphs/Graph.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; @@ -90,6 +106,8 @@ namespace Tensorflow public Graph() { + c_api_util.DownloadLibrary(); + _handle = c_api.TF_NewGraph(); Status = new Status(); _nodes_by_id = new Dictionary(); @@ -100,6 +118,8 @@ namespace Tensorflow public Graph(IntPtr handle) { + c_api_util.DownloadLibrary(); + _handle = handle; Status = new Status(); _nodes_by_id = new Dictionary(); diff --git a/src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs b/src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs index 45bf2a4c..2c1ac0a0 100644 --- a/src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs +++ b/src/TensorFlowNET.Core/Graphs/ImportGraphDefOptions.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/_ControlDependenciesController.cs b/src/TensorFlowNET.Core/Graphs/_ControlDependenciesController.cs index 047624d5..b1337717 100644 --- a/src/TensorFlowNET.Core/Graphs/_ControlDependenciesController.cs +++ b/src/TensorFlowNET.Core/Graphs/_ControlDependenciesController.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Eager; diff --git a/src/TensorFlowNET.Core/Graphs/c_api.graph.cs b/src/TensorFlowNET.Core/Graphs/c_api.graph.cs index afc8ce1f..3bcb3321 100644 --- a/src/TensorFlowNET.Core/Graphs/c_api.graph.cs +++ b/src/TensorFlowNET.Core/Graphs/c_api.graph.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Graphs/graph_io.py.cs b/src/TensorFlowNET.Core/Graphs/graph_io.py.cs index 7abc4cab..162e9ee1 100644 --- a/src/TensorFlowNET.Core/Graphs/graph_io.py.cs +++ b/src/TensorFlowNET.Core/Graphs/graph_io.py.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.IO; diff --git a/src/TensorFlowNET.Core/IO/gfile.cs b/src/TensorFlowNET.Core/IO/gfile.cs index 4f346bb7..cadeb3ad 100644 --- a/src/TensorFlowNET.Core/IO/gfile.cs +++ b/src/TensorFlowNET.Core/IO/gfile.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Text; diff --git a/src/TensorFlowNET.Core/IPyClass.cs b/src/TensorFlowNET.Core/IPyClass.cs index fd08ab82..d89da986 100644 --- a/src/TensorFlowNET.Core/IPyClass.cs +++ b/src/TensorFlowNET.Core/IPyClass.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/ITensorOrOperation.cs b/src/TensorFlowNET.Core/ITensorOrOperation.cs index 511fd116..a74291c5 100644 --- a/src/TensorFlowNET.Core/ITensorOrOperation.cs +++ b/src/TensorFlowNET.Core/ITensorOrOperation.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/BackendBase.cs b/src/TensorFlowNET.Core/Keras/BackendBase.cs index f3624485..df04d138 100644 --- a/src/TensorFlowNET.Core/Keras/BackendBase.cs +++ b/src/TensorFlowNET.Core/Keras/BackendBase.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs b/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs index c4980e92..00ae9955 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/InputSpec.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Engine/Network.cs b/src/TensorFlowNET.Core/Keras/Engine/Network.cs index e06d4e05..f384c289 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Network.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Network.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras.Layers; diff --git a/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs b/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs index bbff5da3..4b6a6f28 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/Sequential.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras.Layers; diff --git a/src/TensorFlowNET.Core/Keras/Initializers.cs b/src/TensorFlowNET.Core/Keras/Initializers.cs index 27cd384e..66845f78 100644 --- a/src/TensorFlowNET.Core/Keras/Initializers.cs +++ b/src/TensorFlowNET.Core/Keras/Initializers.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations.Initializers; diff --git a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs index d733127f..172c28f2 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Conv.cs b/src/TensorFlowNET.Core/Keras/Layers/Conv.cs index aeb37128..d691bdf8 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Conv.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Conv.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras.Engine; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs b/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs index be6d551b..7314ca7d 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations.Activation; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Dense.cs b/src/TensorFlowNET.Core/Keras/Layers/Dense.cs index 9a3b45ba..d402431a 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Dense.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Dense.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs index 3ea6d65d..18d78192 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Embedding.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Layers/InputLayer.cs b/src/TensorFlowNET.Core/Keras/Layers/InputLayer.cs index 60257ee0..4f383375 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/InputLayer.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/InputLayer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Layer.cs b/src/TensorFlowNET.Core/Keras/Layers/Layer.cs index 6c61b12a..4ee0b3c4 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Layer.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Layer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -161,7 +177,7 @@ namespace Tensorflow.Keras.Layers if (_dtype == TF_DataType.DtInvalid) _dtype = input.dtype; - var input_shapes = input.GetShape(); + var input_shapes = input.TensorShape; build(input_shapes); built = true; } diff --git a/src/TensorFlowNET.Core/Keras/Layers/MaxPooling2D.cs b/src/TensorFlowNET.Core/Keras/Layers/MaxPooling2D.cs index 649c1a33..bdb577e0 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/MaxPooling2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/MaxPooling2D.cs @@ -12,7 +12,7 @@ namespace Tensorflow.Keras.Layers int[] strides, string padding = "valid", string data_format = null, - string name = null) : base(nn.max_pool, pool_size, + string name = null) : base(nn.max_pool_fn, pool_size, strides, padding: padding, data_format: data_format, diff --git a/src/TensorFlowNET.Core/Keras/Layers/Node.cs b/src/TensorFlowNET.Core/Keras/Layers/Node.cs index d4144c62..0621e0d1 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Node.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Node.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs b/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs index 69c4d65c..2f41684a 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/Pooling2D.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Keras.Engine; diff --git a/src/TensorFlowNET.Core/Keras/Sequence.cs b/src/TensorFlowNET.Core/Keras/Sequence.cs index fe6dfc33..67d28bfa 100644 --- a/src/TensorFlowNET.Core/Keras/Sequence.cs +++ b/src/TensorFlowNET.Core/Keras/Sequence.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs b/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs index 5244e8e9..e8d98f29 100644 --- a/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs +++ b/src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Utils/conv_utils.cs b/src/TensorFlowNET.Core/Keras/Utils/conv_utils.cs index 790470ee..f88169e0 100644 --- a/src/TensorFlowNET.Core/Keras/Utils/conv_utils.cs +++ b/src/TensorFlowNET.Core/Keras/Utils/conv_utils.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Utils/generic_utils.cs b/src/TensorFlowNET.Core/Keras/Utils/generic_utils.cs index e6166e24..ce322de0 100644 --- a/src/TensorFlowNET.Core/Keras/Utils/generic_utils.cs +++ b/src/TensorFlowNET.Core/Keras/Utils/generic_utils.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/Utils/tf_utils.cs b/src/TensorFlowNET.Core/Keras/Utils/tf_utils.cs index c57344c2..dae5dd63 100644 --- a/src/TensorFlowNET.Core/Keras/Utils/tf_utils.cs +++ b/src/TensorFlowNET.Core/Keras/Utils/tf_utils.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Keras/backend.cs b/src/TensorFlowNET.Core/Keras/backend.cs index e6c64f90..8626e126 100644 --- a/src/TensorFlowNET.Core/Keras/backend.cs +++ b/src/TensorFlowNET.Core/Keras/backend.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using System.Runtime.CompilerServices; diff --git a/src/TensorFlowNET.Core/Keras/defaultdict.cs b/src/TensorFlowNET.Core/Keras/defaultdict.cs index 849abd00..c6d1db01 100644 --- a/src/TensorFlowNET.Core/Keras/defaultdict.cs +++ b/src/TensorFlowNET.Core/Keras/defaultdict.cs @@ -1,4 +1,20 @@ -using System.Collections.Generic; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System.Collections.Generic; namespace System.Collections.Generic { diff --git a/src/TensorFlowNET.Core/Layers/Layer.cs b/src/TensorFlowNET.Core/Layers/Layer.cs index 463da7dc..bdea58be 100644 --- a/src/TensorFlowNET.Core/Layers/Layer.cs +++ b/src/TensorFlowNET.Core/Layers/Layer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Activation/gen_nn_ops.activations.cs b/src/TensorFlowNET.Core/Operations/Activation/gen_nn_ops.activations.cs index af4df920..06f74da6 100644 --- a/src/TensorFlowNET.Core/Operations/Activation/gen_nn_ops.activations.cs +++ b/src/TensorFlowNET.Core/Operations/Activation/gen_nn_ops.activations.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs index 13f065ab..98d1aa1c 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs index 84651423..c8c52766 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowState.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowState.cs index c87ba1c6..a5fba585 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowState.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowState.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/GradLoopState.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/GradLoopState.cs index e8fda1a0..c8f359c6 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/GradLoopState.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/GradLoopState.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/WhileContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/WhileContext.cs index c2fe376e..8211e542 100644 --- a/src/TensorFlowNET.Core/Operations/ControlFlows/WhileContext.cs +++ b/src/TensorFlowNET.Core/Operations/ControlFlows/WhileContext.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations.ControlFlows; diff --git a/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs index 7d25e05b..76d2554a 100644 --- a/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs +++ b/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs @@ -1,3 +1,19 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + //Base classes for probability distributions. using System; using System.Collections.Generic; diff --git a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs index 29cefda1..be0f41d9 100644 --- a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs +++ b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs @@ -1,3 +1,19 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using System; using System.Collections.Generic; using Tensorflow; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs b/src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs index 5d905583..27571707 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/GlorotUniform.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/IInitializer.cs b/src/TensorFlowNET.Core/Operations/Initializers/IInitializer.cs index 422bf95d..6b0bd5d5 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/IInitializer.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/IInitializer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/Ones.cs b/src/TensorFlowNET.Core/Operations/Initializers/Ones.cs index 750c4ec8..9df67731 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/Ones.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/Ones.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs b/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs index 2055bf83..5f92ef3f 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/TruncatedNormal.cs b/src/TensorFlowNET.Core/Operations/Initializers/TruncatedNormal.cs index ae639c8e..7a20fd5b 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/TruncatedNormal.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/TruncatedNormal.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs b/src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs index 16149261..3bc45ab5 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Initializers/Zeros.cs b/src/TensorFlowNET.Core/Operations/Initializers/Zeros.cs index ca1f42df..b4eeea38 100644 --- a/src/TensorFlowNET.Core/Operations/Initializers/Zeros.cs +++ b/src/TensorFlowNET.Core/Operations/Initializers/Zeros.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/InputList.cs b/src/TensorFlowNET.Core/Operations/InputList.cs index f442e546..2857b162 100644 --- a/src/TensorFlowNET.Core/Operations/InputList.cs +++ b/src/TensorFlowNET.Core/Operations/InputList.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs index c07712a1..238403d2 100644 --- a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; @@ -118,8 +134,8 @@ namespace Tensorflow if(weights > 0) { var weights_tensor = ops.convert_to_tensor(weights); - var labels_rank = labels.GetShape().NDim; - var weights_shape = weights_tensor.GetShape(); + var labels_rank = labels.TensorShape.NDim; + var weights_shape = weights_tensor.TensorShape; var weights_rank = weights_shape.NDim; if (labels_rank > -1 && weights_rank > -1) diff --git a/src/TensorFlowNET.Core/Operations/NnOps/Conv2dParams.cs b/src/TensorFlowNET.Core/Operations/NnOps/Conv2dParams.cs index 37380a2b..4decdd4f 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/Conv2dParams.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/Conv2dParams.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/Convolution.cs b/src/TensorFlowNET.Core/Operations/NnOps/Convolution.cs index 727b40b8..e3467b83 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/Convolution.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/Convolution.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs b/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs index 37434e43..6dd48c14 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/_NonAtrousConvolution.cs b/src/TensorFlowNET.Core/Operations/NnOps/_NonAtrousConvolution.cs index c742884a..1300e3b8 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/_NonAtrousConvolution.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/_NonAtrousConvolution.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/NnOps/_WithSpaceToBatch.cs b/src/TensorFlowNET.Core/Operations/NnOps/_WithSpaceToBatch.cs index 8f77e0ea..d175b746 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/_WithSpaceToBatch.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/_WithSpaceToBatch.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -18,7 +34,7 @@ namespace Tensorflow.Operations string data_format = null) { var dilation_rate_tensor = ops.convert_to_tensor(dilation_rate, TF_DataType.TF_INT32, name: "dilation_rate"); - var rate_shape = dilation_rate_tensor.GetShape(); + var rate_shape = dilation_rate_tensor.TensorShape; var num_spatial_dims = rate_shape.Dimensions[0]; int starting_spatial_dim = -1; if (!string.IsNullOrEmpty(data_format) && data_format.StartsWith("NC")) diff --git a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs index 8dba03b9..7e281d46 100644 --- a/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs +++ b/src/TensorFlowNET.Core/Operations/NnOps/gen_nn_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs index 00be4db8..2e5ec897 100644 --- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.ComponentModel; using System.Dynamic; @@ -94,14 +110,28 @@ namespace Tensorflow if (attrs.ContainsKey(input_arg.TypeAttr)) dtype = (DataType)attrs[input_arg.TypeAttr]; else - if (values is Tensor[] values1) - dtype = values1[0].dtype.as_datatype_enum(); + switch (values) + { + case Tensor[] values1: + dtype = values1[0].dtype.as_datatype_enum(); + break; + case object[] values1: + foreach(var t in values1) + if(t is Tensor tensor) + { + dtype = tensor.dtype.as_datatype_enum(); + break; + } + break; + default: + throw new NotImplementedException($"can't infer the dtype for {values.GetType()}"); + } if (dtype == DataType.DtInvalid && default_type_attr_map.ContainsKey(input_arg.TypeAttr)) default_dtype = (DataType)default_type_attr_map[input_arg.TypeAttr]; } - if(input_arg.IsRef && dtype != DataType.DtInvalid) + if(!input_arg.IsRef && dtype != DataType.DtInvalid) dtype = dtype.as_base_dtype(); values = ops.internal_convert_n_to_tensor(values, diff --git a/src/TensorFlowNET.Core/Operations/Operation.Control.cs b/src/TensorFlowNET.Core/Operations/Operation.Control.cs index 4f1c156f..48555bf9 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Control.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Control.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations; diff --git a/src/TensorFlowNET.Core/Operations/Operation.Implicit.cs b/src/TensorFlowNET.Core/Operations/Operation.Implicit.cs index 013c193c..5be78704 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Implicit.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Implicit.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/Operation.Input.cs b/src/TensorFlowNET.Core/Operations/Operation.Input.cs index 349a7603..b5dd724d 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Input.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Input.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index 52cfa2ad..2f5da994 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -1,6 +1,19 @@ -#if GRAPH_SERIALIZE -using Newtonsoft.Json; -#endif +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using System; using System.Collections.Generic; using System.Linq; @@ -17,9 +30,7 @@ namespace Tensorflow private Tensor[] _outputs; public Tensor[] outputs => _outputs; -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif + public Tensor output => _outputs.FirstOrDefault(); public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle); diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index c5bd77f6..0baa6228 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -1,7 +1,20 @@ -using Google.Protobuf.Collections; -#if GRAPH_SERIALIZE -using Newtonsoft.Json; -#endif +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf.Collections; using System; using System.Collections.Generic; using System.Linq; @@ -37,21 +50,11 @@ namespace Tensorflow private Graph _graph; public string type => OpType; -#if GRAPH_SERIALIZE - [JsonIgnore] - public Graph graph => _graph; - [JsonIgnore] - public int _id => _id_value; - [JsonIgnore] - public int _id_value; - [JsonIgnore] - public Operation op => this; -#else public Graph graph => _graph; public int _id => _id_value; public int _id_value; public Operation op => this; -#endif + public TF_DataType dtype => TF_DataType.DtInvalid; private Status status = new Status(); @@ -60,9 +63,6 @@ namespace Tensorflow public string Device => c_api.StringPiece(c_api.TF_OperationDevice(_handle)); private NodeDef _node_def; -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif public NodeDef node_def { get diff --git a/src/TensorFlowNET.Core/Operations/OperationDescription.cs b/src/TensorFlowNET.Core/Operations/OperationDescription.cs index 2962d913..489d6fc7 100644 --- a/src/TensorFlowNET.Core/Operations/OperationDescription.cs +++ b/src/TensorFlowNET.Core/Operations/OperationDescription.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/_UserDeviceSpec.cs b/src/TensorFlowNET.Core/Operations/_UserDeviceSpec.cs index 7a7cb72f..e027658b 100644 --- a/src/TensorFlowNET.Core/Operations/_UserDeviceSpec.cs +++ b/src/TensorFlowNET.Core/Operations/_UserDeviceSpec.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/array_ops.py.cs b/src/TensorFlowNET.Core/Operations/array_ops.py.cs index 262752b2..53afc40c 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.py.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; @@ -492,13 +508,18 @@ namespace Tensorflow { return with(ops.name_scope(name), scope => { var t = ops.convert_to_tensor(axis, name: "concat_dim", dtype: TF_DataType.TF_INT32); - return identity(values[0], name = scope); + return identity(values[0], name: scope); }); } return gen_array_ops.concat_v2(values, axis, name: name); } + public static Tensor concat(object[] values, int axis, string name = "concat") + { + return gen_array_ops.concat_v2(values, axis, name: name); + } + public static Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0) => gen_array_ops.gather_v2(@params, indices, axis, name: name); diff --git a/src/TensorFlowNET.Core/Operations/c_api.ops.cs b/src/TensorFlowNET.Core/Operations/c_api.ops.cs index ebe28114..56f0c5b8 100644 --- a/src/TensorFlowNET.Core/Operations/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Operations/c_api.ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/check_ops.cs b/src/TensorFlowNET.Core/Operations/check_ops.cs index 8c2e335d..37325ad4 100644 --- a/src/TensorFlowNET.Core/Operations/check_ops.cs +++ b/src/TensorFlowNET.Core/Operations/check_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/confusion_matrix.py.cs b/src/TensorFlowNET.Core/Operations/confusion_matrix.py.cs index 0cb54647..f7c7b72c 100644 --- a/src/TensorFlowNET.Core/Operations/confusion_matrix.py.cs +++ b/src/TensorFlowNET.Core/Operations/confusion_matrix.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; @@ -24,9 +40,9 @@ namespace Tensorflow { predictions = ops.convert_to_tensor(predictions); labels = ops.convert_to_tensor(labels); - var predictions_shape = predictions.GetShape(); + var predictions_shape = predictions.TensorShape; var predictions_rank = predictions_shape.NDim; - var labels_shape = labels.GetShape(); + var labels_shape = labels.TensorShape; var labels_rank = labels_shape.NDim; if(labels_rank > -1 && predictions_rank > -1) { diff --git a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs index d543b380..f229a795 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs b/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs index 5e2fc43e..70248873 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_util.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations; diff --git a/src/TensorFlowNET.Core/Operations/embedding_ops.cs b/src/TensorFlowNET.Core/Operations/embedding_ops.cs index 5e7e9906..2a9b5f87 100644 --- a/src/TensorFlowNET.Core/Operations/embedding_ops.cs +++ b/src/TensorFlowNET.Core/Operations/embedding_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs index 8308d48d..b89462a7 100644 --- a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.IO; @@ -19,7 +35,7 @@ namespace Tensorflow /// /// /// - public static Tensor concat_v2(Tensor[] values, int axis, string name = null) + public static Tensor concat_v2(T[] values, int axis, string name = null) { var _op = _op_def_lib._apply_op_helper("ConcatV2", name: name, args: new { values, axis }); diff --git a/src/TensorFlowNET.Core/Operations/gen_control_flow_ops.py.cs b/src/TensorFlowNET.Core/Operations/gen_control_flow_ops.py.cs index 5cb34c59..b5508893 100644 --- a/src/TensorFlowNET.Core/Operations/gen_control_flow_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/gen_control_flow_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/gen_data_flow_ops.py.cs b/src/TensorFlowNET.Core/Operations/gen_data_flow_ops.py.cs index 3cef97ef..63006806 100644 --- a/src/TensorFlowNET.Core/Operations/gen_data_flow_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/gen_data_flow_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/gen_image_ops.py.cs b/src/TensorFlowNET.Core/Operations/gen_image_ops.py.cs index 666e3f29..15cfb10e 100644 --- a/src/TensorFlowNET.Core/Operations/gen_image_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/gen_image_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/gen_io_ops.py.cs b/src/TensorFlowNET.Core/Operations/gen_io_ops.py.cs index c405b9c5..605bd46b 100644 --- a/src/TensorFlowNET.Core/Operations/gen_io_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/gen_io_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs index 8dfbf8f0..24615f99 100644 --- a/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_logging_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs index 3dee5e9e..82aa75f6 100644 --- a/src/TensorFlowNET.Core/Operations/gen_math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_math_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -471,6 +487,41 @@ namespace Tensorflow return _op.outputs[0]; } + /// + /// Multiply slices of the two matrices "x" and "y". + /// + /// + /// The `BatchMatMul` operation is embedded into the + /// `MatMul` operation on the DLL side. However the expected + /// attributes are not the same, hence we need to expose this + /// method to have the right args list on the `_apply_op_helper` + /// function. + /// + /// For each rank > 2 the first rank - 2 dimensions are considered + /// as fixed, and have to be consistent across the two matrices. A + /// common matrix multiplication is then applied over the residual + /// 2 dimensions. + /// + /// e.g. + /// x is (3, 6, 12); y is (3, 12, 6) + /// batch_matmul(x, y) ==> (3, 6, 6) + /// + /// + /// + /// + /// + /// + /// + public static Tensor batch_mat_mul(Tensor x, Tensor y, bool adj_x = false, bool adj_y = false, string name = null) + { + var _op = _op_def_lib._apply_op_helper( + "BatchMatMul", + name, + args: new { x, y, adj_x, adj_y }); + + return _op.outputs[0]; + } + /// /// Returns the max of x and y (i.e. x > y ? x : y) element-wise. /// diff --git a/src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs b/src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs index 1e0d3b30..05faf804 100644 --- a/src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/gen_random_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs index b4173f28..319090e4 100644 --- a/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_resource_variable_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index 29e9d671..9f310bdb 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; @@ -497,6 +513,25 @@ namespace Tensorflow return result; } + public static Tensor batch_matmul(Tensor x, Tensor y, + bool adj_x = false, bool adj_y = false, + string name = null) + { + Tensor result = null; + + with(ops.name_scope(name, "MatMul", new Tensor[] { x, y }), scope => + { + name = scope; + + x = ops.convert_to_tensor(x, name: "a"); + y = ops.convert_to_tensor(y, name: "b"); + + result = gen_math_ops.batch_mat_mul(x, y, adj_x, adj_y, name); + }); + + return result; + } + /// /// Returns the complex conjugate of a complex number. /// diff --git a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs index 8a75c506..5bad8f35 100644 --- a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations; @@ -136,13 +152,13 @@ namespace Tensorflow size = gen_math_ops.less_equal(size, dtypes.int32.max()); Tensor num_nonzero = control_flow_ops.cond( size, - () => math_ops.cast(_count_nonzero(value, dtype: dtypes.int32)), + () => math_ops.cast(_count_nonzero(value, dtype: dtypes.int32), TF_DataType.TF_INT64), () => _count_nonzero(value, dtype: dtypes.int64) ); with(ops.name_scope("counts_to_fraction"), count_scope => { - var num_zero = size - num_nonzero; + var num_zero = math_ops.subtract(math_ops.cast(size, TF_DataType.TF_INT64), num_nonzero); var num_zero_float32 = math_ops.cast(num_zero, dtype: dtypes.float32); var size_float32 = math_ops.cast(size, dtype: dtypes.float32); zero_fraction_float32 = num_zero_float32 / size_float32; diff --git a/src/TensorFlowNET.Core/Operations/nn_ops.cs b/src/TensorFlowNET.Core/Operations/nn_ops.cs index 69d5f166..492e3ca7 100644 --- a/src/TensorFlowNET.Core/Operations/nn_ops.cs +++ b/src/TensorFlowNET.Core/Operations/nn_ops.cs @@ -1,5 +1,22 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; +using System.Linq; using System.Text; using Tensorflow.Operations; using static Tensorflow.Python; @@ -83,7 +100,7 @@ namespace Tensorflow // float to be selected, hence we use a >= comparison. var keep_mask = random_tensor >= rate; var ret = x * scale * math_ops.cast(keep_mask, x.dtype); - ret.SetShape(x.GetShape()); + ret.SetShape(x.TensorShape); return ret; }); } @@ -101,6 +118,38 @@ namespace Tensorflow return _softmax(logits, gen_nn_ops.log_softmax, axis, name); } + /// + /// Performs the max pooling on the input. + /// + /// A 4-D `Tensor` of the format specified by `data_format`. + /// + /// A list or tuple of 4 ints. The size of the window for each dimension + /// of the input tensor. + /// + /// + /// A list or tuple of 4 ints. The stride of the sliding window for + /// each dimension of the input tensor. + /// + /// A string, either `'VALID'` or `'SAME'`. The padding algorithm. + /// A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported. + /// Optional name for the operation. + /// + public static Tensor max_pool(Tensor value, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string name = null) + { + return with(ops.name_scope(name, "MaxPool", value), scope => + { + name = scope; + value = ops.convert_to_tensor(value, name: "input"); + return gen_nn_ops.max_pool( + value, + ksize: ksize, + strides: strides, + padding: padding, + data_format: data_format, + name: name); + }); + } + public static Tensor _softmax(Tensor logits, Func compute_op, int dim = -1, string name = null) { logits = ops.convert_to_tensor(logits); @@ -131,14 +180,14 @@ namespace Tensorflow var precise_logits = logits.dtype == TF_DataType.TF_HALF ? math_ops.cast(logits, dtypes.float32) : logits; // Store label shape for result later. - var labels_static_shape = labels.GetShape(); + var labels_static_shape = labels.TensorShape; var labels_shape = array_ops.shape(labels); /*bool static_shapes_fully_defined = ( labels_static_shape.is_fully_defined() && logits.get_shape()[:-1].is_fully_defined());*/ // Check if no reshapes are required. - if(logits.GetShape().NDim == 2) + if(logits.TensorShape.NDim == 2) { var (cost, _) = gen_nn_ops.sparse_softmax_cross_entropy_with_logits( precise_logits, labels, name: name); @@ -159,17 +208,22 @@ namespace Tensorflow int axis = -1, string name = null) { - return Python.with(ops.name_scope(name, "softmax_cross_entropy_with_logits", new { }), scope => + return with(ops.name_scope(name, "softmax_cross_entropy_with_logits", new { logits, labels }), scope => { + name = scope; var precise_logits = logits; var input_rank = array_ops.rank(precise_logits); - var shape = logits.GetShape(); + var shape = logits.TensorShape; if (axis != -1) throw new NotImplementedException("softmax_cross_entropy_with_logits_v2_helper axis != -1"); var input_shape = array_ops.shape(precise_logits); + // Make precise_logits and labels into matrices. + precise_logits = _flatten_outer_dims(precise_logits); + labels = _flatten_outer_dims(labels); + // Do the actual op computation. // The second output tensor contains the gradients. We use it in // _CrossEntropyGrad() in nn_grad but not here. @@ -186,5 +240,50 @@ namespace Tensorflow return cost; }); } + + /// + /// Flattens logits' outer dimensions and keep its last dimension. + /// + /// + /// + private static Tensor _flatten_outer_dims(Tensor logits) + { + var rank = array_ops.rank(logits); + var last_dim_size = array_ops.slice(array_ops.shape(logits), + new[] { math_ops.subtract(rank, 1) }, + new[] { 1 }); + + var ops = array_ops.concat(new[] { new[] { -1 }, (object)last_dim_size }, 0); + var output = array_ops.reshape(logits, ops); + + // Set output shape if known. + // if not context.executing_eagerly(): + var shape = logits.TensorShape; + if(shape != null && shape.NDim > 0) + { + var product = 1; + var product_valid = true; + foreach(var d in shape.Dimensions.Take(shape.NDim - 1)) + { + if(d == -1) + { + product_valid = false; + break; + } + else + { + product *= d; + } + } + + if (product_valid) + { + var output_shape = new[] { product }; + throw new NotImplementedException("_flatten_outer_dims product_valid"); + } + } + + return output; + } } } diff --git a/src/TensorFlowNET.Core/Operations/random_ops.py.cs b/src/TensorFlowNET.Core/Operations/random_ops.py.cs index ca06ac9f..389e46c9 100644 --- a/src/TensorFlowNET.Core/Operations/random_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/random_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Operations/resource_variable_ops.cs b/src/TensorFlowNET.Core/Operations/resource_variable_ops.cs index eb48c5bc..9dce8407 100644 --- a/src/TensorFlowNET.Core/Operations/resource_variable_ops.cs +++ b/src/TensorFlowNET.Core/Operations/resource_variable_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Operations/weights_broadcast_ops.cs b/src/TensorFlowNET.Core/Operations/weights_broadcast_ops.cs index f0afa1fe..706d1824 100644 --- a/src/TensorFlowNET.Core/Operations/weights_broadcast_ops.cs +++ b/src/TensorFlowNET.Core/Operations/weights_broadcast_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; @@ -16,8 +32,8 @@ namespace Tensorflow weights, dtype: values.dtype.as_base_dtype(), name: "weights"); // Try static check for exact match. - var weights_shape = weights.GetShape(); - var values_shape = values.GetShape(); + var weights_shape = weights.TensorShape; + var values_shape = values.TensorShape; if (weights_shape.is_fully_defined() && values_shape.is_fully_defined()) return weights; diff --git a/src/TensorFlowNET.Core/Python.cs b/src/TensorFlowNET.Core/Python.cs index 595fdd02..ce9a5bde 100644 --- a/src/TensorFlowNET.Core/Python.cs +++ b/src/TensorFlowNET.Core/Python.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/TensorFlowNET.Core/Sessions/BaseSession.cs b/src/TensorFlowNET.Core/Sessions/BaseSession.cs index b1770576..66a1952e 100644 --- a/src/TensorFlowNET.Core/Sessions/BaseSession.cs +++ b/src/TensorFlowNET.Core/Sessions/BaseSession.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/TensorFlowNET.Core/Sessions/Session.cs b/src/TensorFlowNET.Core/Sessions/Session.cs index 01db269b..865e9208 100644 --- a/src/TensorFlowNET.Core/Sessions/Session.cs +++ b/src/TensorFlowNET.Core/Sessions/Session.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/SessionOptions.cs b/src/TensorFlowNET.Core/Sessions/SessionOptions.cs index c6684390..da5b978a 100644 --- a/src/TensorFlowNET.Core/Sessions/SessionOptions.cs +++ b/src/TensorFlowNET.Core/Sessions/SessionOptions.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.Runtime.InteropServices; diff --git a/src/TensorFlowNET.Core/Sessions/TF_DeprecatedSession.cs b/src/TensorFlowNET.Core/Sessions/TF_DeprecatedSession.cs index 5d483ea1..23b9476f 100644 --- a/src/TensorFlowNET.Core/Sessions/TF_DeprecatedSession.cs +++ b/src/TensorFlowNET.Core/Sessions/TF_DeprecatedSession.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/_ElementFetchMapper.cs b/src/TensorFlowNET.Core/Sessions/_ElementFetchMapper.cs index 9a233524..fb053f2e 100644 --- a/src/TensorFlowNET.Core/Sessions/_ElementFetchMapper.cs +++ b/src/TensorFlowNET.Core/Sessions/_ElementFetchMapper.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs b/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs index 19383183..41b54ea5 100644 --- a/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs +++ b/src/TensorFlowNET.Core/Sessions/_FetchHandler.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs b/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs index b8188985..9dc2f50c 100644 --- a/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs +++ b/src/TensorFlowNET.Core/Sessions/_FetchMapper.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs b/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs index c80e3f9e..ad54e1a9 100644 --- a/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs +++ b/src/TensorFlowNET.Core/Sessions/_ListFetchMapper.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/c_api.session.cs b/src/TensorFlowNET.Core/Sessions/c_api.session.cs index 0715f791..95ac835a 100644 --- a/src/TensorFlowNET.Core/Sessions/c_api.session.cs +++ b/src/TensorFlowNET.Core/Sessions/c_api.session.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs b/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs index e657210e..da519feb 100644 --- a/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs +++ b/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Status/Status.cs b/src/TensorFlowNET.Core/Status/Status.cs index e5d11f8a..fb0a9177 100644 --- a/src/TensorFlowNET.Core/Status/Status.cs +++ b/src/TensorFlowNET.Core/Status/Status.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; @@ -24,6 +40,7 @@ namespace Tensorflow public Status() { + c_api_util.DownloadLibrary(); _handle = c_api.TF_NewStatus(); } diff --git a/src/TensorFlowNET.Core/Status/c_api.status.cs b/src/TensorFlowNET.Core/Status/c_api.status.cs index 855a638e..41aacb53 100644 --- a/src/TensorFlowNET.Core/Status/c_api.status.cs +++ b/src/TensorFlowNET.Core/Status/c_api.status.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Summaries/EventFileWriter.cs b/src/TensorFlowNET.Core/Summaries/EventFileWriter.cs index 30903137..6e7be5e7 100644 --- a/src/TensorFlowNET.Core/Summaries/EventFileWriter.cs +++ b/src/TensorFlowNET.Core/Summaries/EventFileWriter.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Text; diff --git a/src/TensorFlowNET.Core/Summaries/EventLoggerThread.cs b/src/TensorFlowNET.Core/Summaries/EventLoggerThread.cs index 1ebd6de5..d2eea494 100644 --- a/src/TensorFlowNET.Core/Summaries/EventLoggerThread.cs +++ b/src/TensorFlowNET.Core/Summaries/EventLoggerThread.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.IO; diff --git a/src/TensorFlowNET.Core/Summaries/EventsWriter.cs b/src/TensorFlowNET.Core/Summaries/EventsWriter.cs index 2cae7ade..9a1701ce 100644 --- a/src/TensorFlowNET.Core/Summaries/EventsWriter.cs +++ b/src/TensorFlowNET.Core/Summaries/EventsWriter.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Text; diff --git a/src/TensorFlowNET.Core/Summaries/FileWriter.cs b/src/TensorFlowNET.Core/Summaries/FileWriter.cs index 2fe3cbf8..60949706 100644 --- a/src/TensorFlowNET.Core/Summaries/FileWriter.cs +++ b/src/TensorFlowNET.Core/Summaries/FileWriter.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Summaries/Summary.cs b/src/TensorFlowNET.Core/Summaries/Summary.cs index 4fcc5666..00c25992 100644 --- a/src/TensorFlowNET.Core/Summaries/Summary.cs +++ b/src/TensorFlowNET.Core/Summaries/Summary.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Summaries/SummaryToEventTransformer.cs b/src/TensorFlowNET.Core/Summaries/SummaryToEventTransformer.cs index 7dc4addf..6a7368f0 100644 --- a/src/TensorFlowNET.Core/Summaries/SummaryToEventTransformer.cs +++ b/src/TensorFlowNET.Core/Summaries/SummaryToEventTransformer.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index f1ee0cdd..b929f5fe 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -5,7 +5,7 @@ TensorFlow.NET Tensorflow 1.14.0 - 0.8.3 + 0.9.0 Haiping Chen, Meinrad Recheis SciSharp STACK true @@ -16,17 +16,18 @@ https://avatars3.githubusercontent.com/u/44989469?s=200&v=4 TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C# Google's TensorFlow full binding in .NET Standard. -Docs: https://tensorflownet.readthedocs.io - 0.8.3.0 - Changes since v0.8: +Docs: https://tensorflownet.readthedocs.io +Learn more about .NET AI: https://medium.com/scisharp + 0.9.0.0 + Changes since v0.8.3: -1. Remove global static graph instance. -2. Provide custom gradient function. -3. Add gradient function for Conv2D. -4. Fix bug for Transfer Learning example. -5. Overload Graph.Import(byte[] bytes) +1. Added full connected Neural Network example. +2. Added word CNN Text Classification example. +3. Fixed AdaOptimizer issue. 7.2 - 0.8.3.0 + 0.9.0.0 + LICENSE + false @@ -42,6 +43,10 @@ Docs: https://tensorflownet.readthedocs.io + + True + + @@ -54,7 +59,6 @@ Docs: https://tensorflownet.readthedocs.io - diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs index 0b64a212..81c8006b 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs index c69419ec..2f4b5d26 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs index 501d2575..aa3e6bb3 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 6e1a63ba..786daa5c 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -1,6 +1,19 @@ -#if GRAPH_SERIALIZE -using Newtonsoft.Json; -#endif +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using NumSharp; using System; using System.Collections.Generic; @@ -22,21 +35,11 @@ namespace Tensorflow private int _id; private Operation _op; -#if GRAPH_SERIALIZE - [JsonIgnore] - public int Id => _id; - [JsonIgnore] - public Graph graph => op?.graph; - [JsonIgnore] - public Operation op => _op; - [JsonIgnore] - public Tensor[] outputs => op.outputs; -#else + public int Id => _id; public Graph graph => op?.graph; public Operation op => _op; public Tensor[] outputs => op.outputs; -#endif /// /// The string name of this tensor. @@ -50,18 +53,12 @@ namespace Tensorflow private TF_DataType _dtype = TF_DataType.DtInvalid; public TF_DataType dtype => _handle == IntPtr.Zero ? _dtype : c_api.TF_TensorType(_handle); -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif + public ulong bytesize => _handle == IntPtr.Zero ? 0 : c_api.TF_TensorByteSize(_handle); -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif + public ulong itemsize => _handle == IntPtr.Zero ? 0 : c_api.TF_DataTypeSize(dtype); public ulong size => _handle == IntPtr.Zero ? 0 : bytesize / itemsize; -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif + public IntPtr buffer => _handle == IntPtr.Zero ? IntPtr.Zero : c_api.TF_TensorData(_handle); public int num_consumers(TF_Output oper_out) => _handle == IntPtr.Zero ? 0 : c_api.TF_OperationOutputNumConsumers(oper_out); @@ -70,9 +67,6 @@ namespace Tensorflow /// /// used for keep other pointer when do implicit operating /// -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif public object Tag { get; set; } public int[] shape @@ -110,10 +104,7 @@ namespace Tensorflow return shape.Select(x => (int)x).ToArray(); } - public TensorShape GetShape() - { - return tensor_util.to_shape(shape); - } + public TensorShape TensorShape => tensor_util.to_shape(shape); public void SetShape(Shape shape) { @@ -143,9 +134,7 @@ namespace Tensorflow } } } -#if GRAPH_SERIALIZE - [JsonIgnore] -#endif + public int NDims => rank; public string Device => op.Device; diff --git a/src/TensorFlowNET.Core/Tensors/TensorShape.cs b/src/TensorFlowNET.Core/Tensors/TensorShape.cs index 9498cead..732e0a4e 100644 --- a/src/TensorFlowNET.Core/Tensors/TensorShape.cs +++ b/src/TensorFlowNET.Core/Tensors/TensorShape.cs @@ -24,6 +24,16 @@ namespace Tensorflow } + public TensorShape this[Slice slice] + { + get + { + return new TensorShape(Dimensions.Skip(slice.Start.Value) + .Take(slice.Length.Value) + .ToArray()); + } + } + /// /// Returns True iff `self` is fully defined in every dimension. /// @@ -37,5 +47,10 @@ namespace Tensorflow { throw new NotImplementedException("TensorShape is_compatible_with"); } + + public static implicit operator TensorShape(int[] dims) => new TensorShape(dims); + public static implicit operator TensorShape((int, int) dims) => new TensorShape(dims.Item1, dims.Item2); + public static implicit operator TensorShape((int, int, int) dims) => new TensorShape(dims.Item1, dims.Item2, dims.Item3); + public static implicit operator TensorShape((int, int, int, int) dims) => new TensorShape(dims.Item1, dims.Item2, dims.Item3, dims.Item4); } } diff --git a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs index 875e8a0a..ea8f715a 100644 --- a/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/c_api.tensor.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/Tensors/constant_op.cs b/src/TensorFlowNET.Core/Tensors/constant_op.cs index 0d4ef0b4..348a4551 100644 --- a/src/TensorFlowNET.Core/Tensors/constant_op.cs +++ b/src/TensorFlowNET.Core/Tensors/constant_op.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Tensors/dtypes.cs b/src/TensorFlowNET.Core/Tensors/dtypes.cs index 2b067fe1..ebe6e283 100644 --- a/src/TensorFlowNET.Core/Tensors/dtypes.cs +++ b/src/TensorFlowNET.Core/Tensors/dtypes.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Tensors/tensor_util.cs b/src/TensorFlowNET.Core/Tensors/tensor_util.cs index a02e1908..85fc2518 100644 --- a/src/TensorFlowNET.Core/Tensors/tensor_util.cs +++ b/src/TensorFlowNET.Core/Tensors/tensor_util.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Tensors/tf.constant.cs b/src/TensorFlowNET.Core/Tensors/tf.constant.cs index c4c5ffaf..3f67589b 100644 --- a/src/TensorFlowNET.Core/Tensors/tf.constant.cs +++ b/src/TensorFlowNET.Core/Tensors/tf.constant.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; @@ -20,6 +36,15 @@ namespace Tensorflow verify_shape: verify_shape, allow_broadcast: false); + public static Tensor constant(float value, + int shape, + string name = "Const") => constant_op._constant_impl(value, + tf.float32, + new int[] { shape }, + name, + verify_shape: false, + allow_broadcast: false); + public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) => array_ops.zeros(shape, dtype, name); public static Tensor size(Tensor input, diff --git a/src/TensorFlowNET.Core/Train/AdamOptimizer.cs b/src/TensorFlowNET.Core/Train/AdamOptimizer.cs index 15557679..ce4277b3 100644 --- a/src/TensorFlowNET.Core/Train/AdamOptimizer.cs +++ b/src/TensorFlowNET.Core/Train/AdamOptimizer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -110,7 +126,7 @@ namespace Tensorflow.Train var update_beta2 = beta2_power.assign(beta2_power * _beta2_t, use_locking: _use_locking); operations.Add(update_beta1); - operations.Add(update_beta1); + operations.Add(update_beta2); }); return control_flow_ops.group(operations.ToArray(), name: name_scope); diff --git a/src/TensorFlowNET.Core/Train/GradientDescentOptimizer.cs b/src/TensorFlowNET.Core/Train/GradientDescentOptimizer.cs index d69228d6..f8e06926 100644 --- a/src/TensorFlowNET.Core/Train/GradientDescentOptimizer.cs +++ b/src/TensorFlowNET.Core/Train/GradientDescentOptimizer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Optimizer.cs b/src/TensorFlowNET.Core/Train/Optimizer.cs index 0ce5225f..6c13ac20 100644 --- a/src/TensorFlowNET.Core/Train/Optimizer.cs +++ b/src/TensorFlowNET.Core/Train/Optimizer.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs index 13886401..589f9b6c 100644 --- a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs +++ b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/ISaverBuilder.cs b/src/TensorFlowNET.Core/Train/Saving/ISaverBuilder.cs index d16d2a21..2340e2f5 100644 --- a/src/TensorFlowNET.Core/Train/Saving/ISaverBuilder.cs +++ b/src/TensorFlowNET.Core/Train/Saving/ISaverBuilder.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/ReferenceVariableSaveable.cs b/src/TensorFlowNET.Core/Train/Saving/ReferenceVariableSaveable.cs index 583ef889..fbe9509c 100644 --- a/src/TensorFlowNET.Core/Train/Saving/ReferenceVariableSaveable.cs +++ b/src/TensorFlowNET.Core/Train/Saving/ReferenceVariableSaveable.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/ResourceVariableSaveable.cs b/src/TensorFlowNET.Core/Train/Saving/ResourceVariableSaveable.cs index a504acc6..7c890e9e 100644 --- a/src/TensorFlowNET.Core/Train/Saving/ResourceVariableSaveable.cs +++ b/src/TensorFlowNET.Core/Train/Saving/ResourceVariableSaveable.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/SaveSpec.cs b/src/TensorFlowNET.Core/Train/Saving/SaveSpec.cs index 1e932209..601a022e 100644 --- a/src/TensorFlowNET.Core/Train/Saving/SaveSpec.cs +++ b/src/TensorFlowNET.Core/Train/Saving/SaveSpec.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/SaveableObject.cs b/src/TensorFlowNET.Core/Train/Saving/SaveableObject.cs index 90a4fff7..17329784 100644 --- a/src/TensorFlowNET.Core/Train/Saving/SaveableObject.cs +++ b/src/TensorFlowNET.Core/Train/Saving/SaveableObject.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/Saver.cs b/src/TensorFlowNET.Core/Train/Saving/Saver.cs index b367eb1d..37f5c302 100644 --- a/src/TensorFlowNET.Core/Train/Saving/Saver.cs +++ b/src/TensorFlowNET.Core/Train/Saving/Saver.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/src/TensorFlowNET.Core/Train/Saving/checkpoint_management.py.cs b/src/TensorFlowNET.Core/Train/Saving/checkpoint_management.py.cs index 1ae4c585..6cbe6b4c 100644 --- a/src/TensorFlowNET.Core/Train/Saving/checkpoint_management.py.cs +++ b/src/TensorFlowNET.Core/Train/Saving/checkpoint_management.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/src/TensorFlowNET.Core/Train/Saving/saveable_object_util.py.cs b/src/TensorFlowNET.Core/Train/Saving/saveable_object_util.py.cs index 83031922..668aae36 100644 --- a/src/TensorFlowNET.Core/Train/Saving/saveable_object_util.py.cs +++ b/src/TensorFlowNET.Core/Train/Saving/saveable_object_util.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/Saving/saver.py.cs b/src/TensorFlowNET.Core/Train/Saving/saver.py.cs index c21b954f..455f4e47 100644 --- a/src/TensorFlowNET.Core/Train/Saving/saver.py.cs +++ b/src/TensorFlowNET.Core/Train/Saving/saver.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/SlotCreator.cs b/src/TensorFlowNET.Core/Train/SlotCreator.cs index a666cae0..a055084a 100644 --- a/src/TensorFlowNET.Core/Train/SlotCreator.cs +++ b/src/TensorFlowNET.Core/Train/SlotCreator.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Operations.Initializers; diff --git a/src/TensorFlowNET.Core/Train/Trackable.cs b/src/TensorFlowNET.Core/Train/Trackable.cs index c98b2116..99096618 100644 --- a/src/TensorFlowNET.Core/Train/Trackable.cs +++ b/src/TensorFlowNET.Core/Train/Trackable.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/gen_training_ops.py.cs b/src/TensorFlowNET.Core/Train/gen_training_ops.py.cs index 53726c3f..35e56378 100644 --- a/src/TensorFlowNET.Core/Train/gen_training_ops.py.cs +++ b/src/TensorFlowNET.Core/Train/gen_training_ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Train/optimizer.py.cs b/src/TensorFlowNET.Core/Train/optimizer.py.cs index 15c302b4..39fff271 100644 --- a/src/TensorFlowNET.Core/Train/optimizer.py.cs +++ b/src/TensorFlowNET.Core/Train/optimizer.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Framework; diff --git a/src/TensorFlowNET.Core/Train/tf.optimizers.cs b/src/TensorFlowNET.Core/Train/tf.optimizers.cs index 9e3d66a6..e1802b30 100644 --- a/src/TensorFlowNET.Core/Train/tf.optimizers.cs +++ b/src/TensorFlowNET.Core/Train/tf.optimizers.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -10,9 +26,11 @@ namespace Tensorflow { public static class train { - public static Optimizer GradientDescentOptimizer(float learning_rate) => new GradientDescentOptimizer(learning_rate); + public static Optimizer GradientDescentOptimizer(float learning_rate) + => new GradientDescentOptimizer(learning_rate); - public static Optimizer AdamOptimizer(float learning_rate) => new AdamOptimizer(learning_rate); + public static Optimizer AdamOptimizer(float learning_rate, string name = "Adam") + => new AdamOptimizer(learning_rate, name: name); public static Saver Saver(VariableV1[] var_list = null) => new Saver(var_list: var_list); diff --git a/src/TensorFlowNET.Core/Util/CmdHelper.cs b/src/TensorFlowNET.Core/Util/CmdHelper.cs new file mode 100644 index 00000000..d4ed6cb1 --- /dev/null +++ b/src/TensorFlowNET.Core/Util/CmdHelper.cs @@ -0,0 +1,54 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; + +namespace Tensorflow.Util +{ + public static class CmdHelper + { + public static void Command(string command) + { + Process proc = new System.Diagnostics.Process(); + proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe"; + proc.StartInfo.Arguments = "/c \" " + command + " \""; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.Start(); + + while (!proc.StandardOutput.EndOfStream) + Console.WriteLine(proc.StandardOutput.ReadLine()); + } + + public static void Bash(string command) + { + Process proc = new System.Diagnostics.Process(); + proc.StartInfo.FileName = "/bin/bash"; + proc.StartInfo.Arguments = "-c \" " + command + " \""; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.Start(); + + while (!proc.StandardOutput.EndOfStream) + Console.WriteLine(proc.StandardOutput.ReadLine()); + } + } +} diff --git a/src/TensorFlowNET.Core/Util/nest.py.cs b/src/TensorFlowNET.Core/Util/nest.py.cs index 17b2f556..060539cc 100644 --- a/src/TensorFlowNET.Core/Util/nest.py.cs +++ b/src/TensorFlowNET.Core/Util/nest.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections; using System.Collections.Generic; using System.Linq; diff --git a/src/TensorFlowNET.Core/Variables/PureVariableScope.cs b/src/TensorFlowNET.Core/Variables/PureVariableScope.cs index ff7ae22a..cdbc1d6a 100644 --- a/src/TensorFlowNET.Core/Variables/PureVariableScope.cs +++ b/src/TensorFlowNET.Core/Variables/PureVariableScope.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs index cd5b8680..2ea6b91d 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.cs b/src/TensorFlowNET.Core/Variables/RefVariable.cs index 7b1de63c..df30d042 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using Google.Protobuf.Collections; using System; using System.Collections.Generic; @@ -153,7 +169,7 @@ namespace Tensorflow // Manually overrides the variable's shape with the initial value's. if (validate_shape) { - var initial_value_shape = _initial_value.GetShape(); + var initial_value_shape = _initial_value.TensorShape; if (!initial_value_shape.is_fully_defined()) throw new ValueError($"initial_value must have a shape specified: {_initial_value}"); } diff --git a/src/TensorFlowNET.Core/Variables/ResourceVariable.cs b/src/TensorFlowNET.Core/Variables/ResourceVariable.cs index 57222ba8..5a7491c5 100644 --- a/src/TensorFlowNET.Core/Variables/ResourceVariable.cs +++ b/src/TensorFlowNET.Core/Variables/ResourceVariable.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/VariableScope.cs b/src/TensorFlowNET.Core/Variables/VariableScope.cs index d9816af3..02a7b2d7 100644 --- a/src/TensorFlowNET.Core/Variables/VariableScope.cs +++ b/src/TensorFlowNET.Core/Variables/VariableScope.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Variables/VariableV1.cs b/src/TensorFlowNET.Core/Variables/VariableV1.cs index 1e9aed72..a222c67f 100644 --- a/src/TensorFlowNET.Core/Variables/VariableV1.cs +++ b/src/TensorFlowNET.Core/Variables/VariableV1.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using static Tensorflow.Python; diff --git a/src/TensorFlowNET.Core/Variables/_VariableScopeStore.cs b/src/TensorFlowNET.Core/Variables/_VariableScopeStore.cs index 6a3879db..3f0411a0 100644 --- a/src/TensorFlowNET.Core/Variables/_VariableScopeStore.cs +++ b/src/TensorFlowNET.Core/Variables/_VariableScopeStore.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/_VariableStore.cs b/src/TensorFlowNET.Core/Variables/_VariableStore.cs index 420f929f..69ea7b01 100644 --- a/src/TensorFlowNET.Core/Variables/_VariableStore.cs +++ b/src/TensorFlowNET.Core/Variables/_VariableStore.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs b/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs index e19ace4d..0314e11f 100644 --- a/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs +++ b/src/TensorFlowNET.Core/Variables/gen_state_ops.py.cs @@ -1,4 +1,19 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/state_ops.cs b/src/TensorFlowNET.Core/Variables/state_ops.cs index d5acf767..bc96d1f5 100644 --- a/src/TensorFlowNET.Core/Variables/state_ops.cs +++ b/src/TensorFlowNET.Core/Variables/state_ops.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/Variables/variable_scope.py.cs b/src/TensorFlowNET.Core/Variables/variable_scope.py.cs index b84196a3..11bc7d3d 100644 --- a/src/TensorFlowNET.Core/Variables/variable_scope.py.cs +++ b/src/TensorFlowNET.Core/Variables/variable_scope.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -106,13 +122,13 @@ namespace Tensorflow if (!string.IsNullOrEmpty(_name) || _scope != null) { - var name_scope = _scope.name.Split('/').Last(); + var name_scope = _scope == null ? _name : _scope.name.Split('/').Last(); if (current_name_scope == null) current_name_scope = ops.name_scope(name_scope); current_name_scope.__enter__(); - var current_name_scope_name = current_name_scope; + string current_name_scope_name = current_name_scope; _current_name_scope = current_name_scope; - string old_name_scope = _scope.original_name_scope; + string old_name_scope = _scope == null ? current_name_scope_name : _scope.original_name_scope; if(_scope == null) pure_variable_scope = new PureVariableScope(_name, old_name_scope: old_name_scope); diff --git a/src/TensorFlowNET.Core/Variables/variables.py.cs b/src/TensorFlowNET.Core/Variables/variables.py.cs index 26066d3b..df681f78 100644 --- a/src/TensorFlowNET.Core/Variables/variables.py.cs +++ b/src/TensorFlowNET.Core/Variables/variables.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/WeakKeyDicionary.cs b/src/TensorFlowNET.Core/WeakKeyDicionary.cs index 98df4f30..79b2ec22 100644 --- a/src/TensorFlowNET.Core/WeakKeyDicionary.cs +++ b/src/TensorFlowNET.Core/WeakKeyDicionary.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; diff --git a/src/TensorFlowNET.Core/ops.GraphKeys.cs b/src/TensorFlowNET.Core/ops.GraphKeys.cs index 84f48db5..d55907ad 100644 --- a/src/TensorFlowNET.Core/ops.GraphKeys.cs +++ b/src/TensorFlowNET.Core/ops.GraphKeys.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/ops._DefaultStack.cs b/src/TensorFlowNET.Core/ops._DefaultStack.cs index 35a938f5..45c9b1e5 100644 --- a/src/TensorFlowNET.Core/ops._DefaultStack.cs +++ b/src/TensorFlowNET.Core/ops._DefaultStack.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; diff --git a/src/TensorFlowNET.Core/ops.name_scope.cs b/src/TensorFlowNET.Core/ops.name_scope.cs index 2b1bd021..913d5c34 100644 --- a/src/TensorFlowNET.Core/ops.name_scope.cs +++ b/src/TensorFlowNET.Core/ops.name_scope.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/src/TensorFlowNET.Core/ops.py.cs b/src/TensorFlowNET.Core/ops.py.cs index df389156..93d55975 100644 --- a/src/TensorFlowNET.Core/ops.py.cs +++ b/src/TensorFlowNET.Core/ops.py.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; diff --git a/src/TensorFlowNET.Core/tf.cs b/src/TensorFlowNET.Core/tf.cs index 52c4f988..1abe98bb 100644 --- a/src/TensorFlowNET.Core/tf.cs +++ b/src/TensorFlowNET.Core/tf.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow.Eager; @@ -22,13 +38,13 @@ namespace Tensorflow public static Session defaultSession; - public static RefVariable Variable(T data, + public static RefVariable Variable(T data, bool trainable = true, bool validate_shape = true, - string name = null, + string name = null, TF_DataType dtype = TF_DataType.DtInvalid) { - return Tensorflow.variable_scope.default_variable_creator(data, + return Tensorflow.variable_scope.default_variable_creator(data, trainable: trainable, validate_shape: validate_shape, name: name, @@ -46,7 +62,14 @@ namespace Tensorflow context.default_execution_mode = Context.EAGER_MODE; } - public static string VERSION => c_api.StringPiece(c_api.TF_Version()); + public static string VERSION + { + get + { + c_api_util.DownloadLibrary(); + return c_api.StringPiece(c_api.TF_Version()); + } + } public static Session Session() { diff --git a/tensorflowlib/runtimes/linux-x64/native/libtensorflow.so b/tensorflowlib/runtimes/linux-x64/native/libtensorflow.so deleted file mode 100644 index e69de29b..00000000 diff --git a/tensorflowlib/runtimes/linux-x64/native/libtensorflow_framework.so b/tensorflowlib/runtimes/linux-x64/native/libtensorflow_framework.so deleted file mode 100644 index e69de29b..00000000 diff --git a/tensorflowlib/runtimes/win-x64/native/tensorflow.dll b/tensorflowlib/runtimes/win-x64/native/tensorflow.dll deleted file mode 100644 index e69de29b..00000000 diff --git a/test/KerasNET.Test/BaseTests.cs b/test/KerasNET.Test/BaseTests.cs index 6ab72276..92c777a0 100644 --- a/test/KerasNET.Test/BaseTests.cs +++ b/test/KerasNET.Test/BaseTests.cs @@ -15,8 +15,8 @@ namespace Keras.Test { var dense_1 = new Dense(1, name: "dense_1", activation: tf.nn.relu()); var input = new Tensor(np.array(new int[] { 3 })); - dense_1.__build__(input.GetShape()); - var outputShape = dense_1.output_shape(input.GetShape()); + dense_1.__build__(input.TensorShape); + var outputShape = dense_1.output_shape(input.TensorShape); var a = (int[])(outputShape.Dimensions); var b = (int[])(new int[] { 1 }); var _a = np.array(a); diff --git a/test/KerasNET.Test/Keras.UnitTest.csproj b/test/KerasNET.Test/Keras.UnitTest.csproj index 1e9a2253..a775ad25 100644 --- a/test/KerasNET.Test/Keras.UnitTest.csproj +++ b/test/KerasNET.Test/Keras.UnitTest.csproj @@ -26,7 +26,7 @@ - + diff --git a/test/TensorFlowNET.Examples/BasicEagerApi.cs b/test/TensorFlowNET.Examples/BasicEagerApi.cs index 35664310..8844e17a 100644 --- a/test/TensorFlowNET.Examples/BasicEagerApi.cs +++ b/test/TensorFlowNET.Examples/BasicEagerApi.cs @@ -56,12 +56,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs b/test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs index b8f01865..d60b0f8e 100644 --- a/test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs +++ b/test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Diagnostics; @@ -27,21 +43,55 @@ namespace TensorFlowNET.Examples public int? test_size = null; public int batch_size = 1024; // The number of samples per batch - Datasets mnist; + Datasets mnist; NDArray full_data_x; int num_steps = 20; // Total steps to train int k = 25; // The number of clusters int num_classes = 10; // The 10 digits int num_features = 784; // Each image is 28x28 pixels + float accuray_test = 0f; + public bool Run() { PrepareData(); + var graph = ImportGraph(); + with(tf.Session(graph), sess => + { + Train(sess); + }); + + return accuray_test > 0.70; + } + public void PrepareData() + { + mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size:validation_size, test_size:test_size); + full_data_x = mnist.train.data; + + // download graph meta data + string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/kmeans.meta"; + Web.Download(url, "graph", "kmeans.meta"); + } + + public Graph ImportGraph() + { var graph = tf.Graph().as_default(); tf.train.import_meta_graph("graph/kmeans.meta"); + return graph; + } + + public Graph BuildGraph() + { + throw new NotImplementedException(); + } + + public void Train(Session sess) + { + var graph = tf.Graph(); + // Input images Tensor X = graph.get_operation_by_name("Placeholder"); // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features)); // Labels (for assigning a label to a centroid and testing) @@ -60,89 +110,65 @@ namespace TensorFlowNET.Examples Tensor cluster_idx = graph.get_operation_by_name("Squeeze_1"); NDArray result = null; - with(tf.Session(graph), sess => - { - sess.run(init_vars, new FeedItem(X, full_data_x)); - sess.run(init_op, new FeedItem(X, full_data_x)); - - // Training - var sw = new Stopwatch(); - - foreach (var i in range(1, num_steps + 1)) - { - sw.Restart(); - result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x)); - sw.Stop(); - - if (i % 4 == 0 || i == 1) - print($"Step {i}, Avg Distance: {result[1]} Elapse: {sw.ElapsedMilliseconds}ms"); - } - - var idx = result[2].Data(); - - // Assign a label to each centroid - // Count total number of labels per centroid, using the label of each training - // sample to their closest centroid (given by 'idx') - var counts = np.zeros((k, num_classes), np.float32); - - sw.Start(); - foreach (var i in range(idx.Length)) - { - var x = mnist.train.labels[i]; - counts[idx[i]] += x; - } - - sw.Stop(); - print($"Assign a label to each centroid took {sw.ElapsedMilliseconds}ms"); - - // Assign the most frequent label to the centroid - var labels_map_array = np.argmax(counts, 1); - var labels_map = tf.convert_to_tensor(labels_map_array); - - // Evaluation ops - // Lookup: centroid_id -> label - var cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx); + sess.run(init_vars, new FeedItem(X, full_data_x)); + sess.run(init_op, new FeedItem(X, full_data_x)); - // Compute accuracy - var correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32)); - var cast = tf.cast(correct_prediction, tf.float32); - var accuracy_op = tf.reduce_mean(cast); + // Training + var sw = new Stopwatch(); - // Test Model - var (test_x, test_y) = (mnist.test.images, mnist.test.labels); - result = sess.run(accuracy_op, new FeedItem(X, test_x), new FeedItem(Y, test_y)); - print($"Test Accuracy: {result}"); - }); - - return (float)result > 0.70; - } + foreach (var i in range(1, num_steps + 1)) + { + sw.Restart(); + result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x)); + sw.Stop(); - public void PrepareData() - { - mnist = MnistDataSet.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size:validation_size, test_size:test_size); - full_data_x = mnist.train.images; + if (i % 4 == 0 || i == 1) + print($"Step {i}, Avg Distance: {result[1]} Elapse: {sw.ElapsedMilliseconds}ms"); + } - // download graph meta data - string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/kmeans.meta"; - Web.Download(url, "graph", "kmeans.meta"); - } + var idx = result[2].Data(); - public Graph ImportGraph() - { - throw new NotImplementedException(); - } + // Assign a label to each centroid + // Count total number of labels per centroid, using the label of each training + // sample to their closest centroid (given by 'idx') + var counts = np.zeros((k, num_classes), np.float32); - public Graph BuildGraph() - { - throw new NotImplementedException(); + sw.Start(); + foreach (var i in range(idx.Length)) + { + var x = mnist.train.labels[i]; + counts[idx[i]] += x; + } + + sw.Stop(); + print($"Assign a label to each centroid took {sw.ElapsedMilliseconds}ms"); + + // Assign the most frequent label to the centroid + var labels_map_array = np.argmax(counts, 1); + var labels_map = tf.convert_to_tensor(labels_map_array); + + // Evaluation ops + // Lookup: centroid_id -> label + var cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx); + + // Compute accuracy + var correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32)); + var cast = tf.cast(correct_prediction, tf.float32); + var accuracy_op = tf.reduce_mean(cast); + + // Test Model + var (test_x, test_y) = (mnist.test.data, mnist.test.labels); + result = sess.run(accuracy_op, new FeedItem(X, test_x), new FeedItem(Y, test_y)); + accuray_test = result; + print($"Test Accuracy: {accuray_test}"); } - public bool Train() + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs b/test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs index 59a36530..96c5c6b4 100644 --- a/test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs +++ b/test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; @@ -122,12 +138,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs b/test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs index d8e9444a..d23e7872 100644 --- a/test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs +++ b/test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Diagnostics; @@ -32,7 +48,7 @@ namespace TensorFlowNET.Examples private float learning_rate = 0.01f; private int display_step = 1; - Datasets mnist; + Datasets mnist; public bool Run() { @@ -102,7 +118,7 @@ namespace TensorFlowNET.Examples var correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)); // Calculate accuracy var accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)); - float acc = accuracy.eval(new FeedItem(x, mnist.test.images), new FeedItem(y, mnist.test.labels)); + float acc = accuracy.eval(new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels)); print($"Accuracy: {acc.ToString("F4")}"); return acc > 0.9; @@ -111,7 +127,7 @@ namespace TensorFlowNET.Examples public void PrepareData() { - mnist = MnistDataSet.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size); + mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size); } public void SaveModel(Session sess) @@ -132,30 +148,27 @@ namespace TensorFlowNET.Examples initializer_nodes: ""); } - public void Predict() + public void Predict(Session sess) { var graph = new Graph().as_default(); graph.Import(Path.Join("logistic_regression", "model.pb")); - with(tf.Session(graph), sess => - { - // restoring the model - // var saver = tf.train.import_meta_graph("logistic_regression/tensorflowModel.ckpt.meta"); - // saver.restore(sess, tf.train.latest_checkpoint('logistic_regression')); - var pred = graph.OperationByName("Softmax"); - var output = pred.outputs[0]; - var x = graph.OperationByName("Placeholder"); - var input = x.outputs[0]; - - // predict - var (batch_xs, batch_ys) = mnist.train.next_batch(10); - var results = sess.run(output, new FeedItem(input, batch_xs[np.arange(1)])); - - if (results.argmax() == (batch_ys[0] as NDArray).argmax()) - print("predicted OK!"); - else - throw new ValueError("predict error, should be 90% accuracy"); - }); + // restoring the model + // var saver = tf.train.import_meta_graph("logistic_regression/tensorflowModel.ckpt.meta"); + // saver.restore(sess, tf.train.latest_checkpoint('logistic_regression')); + var pred = graph.OperationByName("Softmax"); + var output = pred.outputs[0]; + var x = graph.OperationByName("Placeholder"); + var input = x.outputs[0]; + + // predict + var (batch_xs, batch_ys) = mnist.train.next_batch(10); + var results = sess.run(output, new FeedItem(input, batch_xs[np.arange(1)])); + + if (results.argmax() == (batch_ys[0] as NDArray).argmax()) + print("predicted OK!"); + else + throw new ValueError("predict error, should be 90% accuracy"); } public Graph ImportGraph() @@ -168,12 +181,12 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - bool IExample.Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs b/test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs index 1f891b8c..ef92055f 100644 --- a/test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs +++ b/test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow; @@ -189,12 +205,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs b/test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs index 07838927..e784eeeb 100644 --- a/test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs +++ b/test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.Text; @@ -17,7 +33,7 @@ namespace TensorFlowNET.Examples { public bool Enabled { get; set; } = true; public string Name => "Nearest Neighbor"; - Datasets mnist; + Datasets mnist; NDArray Xtr, Ytr, Xte, Yte; public int? TrainSize = null; public int ValidationSize = 5000; @@ -70,7 +86,7 @@ namespace TensorFlowNET.Examples public void PrepareData() { - mnist = MnistDataSet.read_data_sets("mnist", one_hot: true, train_size: TrainSize, validation_size:ValidationSize, test_size:TestSize); + mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: TrainSize, validation_size:ValidationSize, test_size:TestSize); // In this example, we limit mnist data (Xtr, Ytr) = mnist.train.next_batch(TrainSize==null ? 5000 : TrainSize.Value / 100); // 5000 for training (nn candidates) (Xte, Yte) = mnist.test.next_batch(TestSize==null ? 200 : TestSize.Value / 100); // 200 for testing @@ -86,12 +102,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs b/test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs index 6b8bf10f..1aa65541 100644 --- a/test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs +++ b/test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs @@ -1,3 +1,19 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using System; using System.Collections.Generic; using System.Text; @@ -162,12 +178,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/BasicOperations.cs b/test/TensorFlowNET.Examples/BasicOperations.cs index ce861df7..8176fb0f 100644 --- a/test/TensorFlowNET.Examples/BasicOperations.cs +++ b/test/TensorFlowNET.Examples/BasicOperations.cs @@ -91,11 +91,69 @@ namespace TensorFlowNET.Examples // graph: the two constants and matmul. // // The output of the op is returned in 'result' as a numpy `ndarray` object. - return with(tf.Session(), sess => + using (sess = tf.Session()) { var result = sess.run(product); Console.WriteLine(result.ToString()); // ==> [[ 12.]] - return result.Data()[0] == 12; + }; + + // `BatchMatMul` is actually embedded into the `MatMul` operation on the tensorflow.dll side. Every time we ask + // for a multiplication between matrices with rank > 2, the first rank - 2 dimensions are checked to be consistent + // across the two matrices and a common matrix multiplication is done on the residual 2 dimensions. + // + // np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(3, 3, 3) + // array([[[1, 2, 3], + // [4, 5, 6], + // [7, 8, 9]], + // + // [[1, 2, 3], + // [4, 5, 6], + // [7, 8, 9]], + // + // [[1, 2, 3], + // [4, 5, 6], + // [7, 8, 9]]]) + var firstTensor = tf.convert_to_tensor( + np.reshape( + np.array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9), + 3, 3, 3)); + // + // np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0]).reshape(3,3,2) + // array([[[0, 1], + // [0, 1], + // [0, 1]], + // + // [[0, 1], + // [0, 0], + // [1, 0]], + // + // [[1, 0], + // [1, 0], + // [1, 0]]]) + var secondTensor = tf.convert_to_tensor( + np.reshape( + np.array(0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0), + 3, 3, 2)); + var batchMul = tf.batch_matmul(firstTensor, secondTensor); + var checkTensor = np.array(0, 6, 0, 15, 0, 24, 3, 1, 6, 4, 9, 7, 6, 0, 15, 0, 24, 0); + return with(tf.Session(), sess => + { + var result = sess.run(batchMul); + Console.WriteLine(result.ToString()); + // + // ==> array([[[0, 6], + // [0, 15], + // [0, 24]], + // + // [[ 3, 1], + // [ 6, 4], + // [ 9, 7]], + // + // [[ 6, 0], + // [15, 0], + // [24, 0]]]) + return np.reshape(result, 18) + .array_equal(checkTensor); }); } @@ -113,12 +171,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/HelloWorld.cs b/test/TensorFlowNET.Examples/HelloWorld.cs index f963e3b3..b6973503 100644 --- a/test/TensorFlowNET.Examples/HelloWorld.cs +++ b/test/TensorFlowNET.Examples/HelloWorld.cs @@ -50,12 +50,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/IExample.cs b/test/TensorFlowNET.Examples/IExample.cs index 3c9c24df..d9826bae 100644 --- a/test/TensorFlowNET.Examples/IExample.cs +++ b/test/TensorFlowNET.Examples/IExample.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; using Tensorflow; @@ -29,9 +45,10 @@ namespace TensorFlowNET.Examples /// Build dataflow graph, train and predict /// /// - bool Train(); + void Train(Session sess); + void Test(Session sess); - bool Predict(); + void Predict(Session sess); Graph ImportGraph(); diff --git a/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionCNN.cs b/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionCNN.cs new file mode 100644 index 00000000..a5a70f92 --- /dev/null +++ b/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionCNN.cs @@ -0,0 +1,305 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; +using TensorFlowNET.Examples.Utility; +using static Tensorflow.Python; + +namespace TensorFlowNET.Examples.ImageProcess +{ + /// + /// Convolutional Neural Network classifier for Hand Written Digits + /// CNN architecture with two convolutional layers, followed by two fully-connected layers at the end. + /// Use Stochastic Gradient Descent (SGD) optimizer. + /// http://www.easy-tensorflow.com/tf-tutorials/convolutional-neural-nets-cnns/cnn1 + /// + public class DigitRecognitionCNN : IExample + { + public bool Enabled { get; set; } = true; + public bool IsImportingGraph { get; set; } = false; + + public string Name => "MNIST CNN"; + + string logs_path = "logs"; + + const int img_h = 28, img_w = 28; // MNIST images are 28x28 + int img_size_flat = img_h * img_w; // 784, the total number of pixels + int n_classes = 10; // Number of classes, one class per digit + int n_channels = 1; + + // Hyper-parameters + int epochs = 10; + int batch_size = 100; + float learning_rate = 0.001f; + Datasets mnist; + + // Network configuration + // 1st Convolutional Layer + int filter_size1 = 5; // Convolution filters are 5 x 5 pixels. + int num_filters1 = 16; // There are 16 of these filters. + int stride1 = 1; // The stride of the sliding window + + // 2nd Convolutional Layer + int filter_size2 = 5; // Convolution filters are 5 x 5 pixels. + int num_filters2 = 32;// There are 32 of these filters. + int stride2 = 1; // The stride of the sliding window + + // Fully-connected layer. + int h1 = 128; // Number of neurons in fully-connected layer. + + + Tensor x, y; + Tensor loss, accuracy, cls_prediction; + Operation optimizer; + + int display_freq = 100; + float accuracy_test = 0f; + float loss_test = 1f; + + public bool Run() + { + PrepareData(); + BuildGraph(); + + with(tf.Session(), sess => + { + Train(sess); + Test(sess); + }); + + return loss_test < 0.09 && accuracy_test > 0.95; + } + + public Graph BuildGraph() + { + var graph = new Graph().as_default(); + + with(tf.name_scope("Input"), delegate + { + // Placeholders for inputs (x) and outputs(y) + x = tf.placeholder(tf.float32, shape: (-1, img_h, img_w, n_channels), name: "X"); + y = tf.placeholder(tf.float32, shape: (-1, n_classes), name: "Y"); + }); + + var conv1 = conv_layer(x, filter_size1, num_filters1, stride1, name: "conv1"); + var pool1 = max_pool(conv1, ksize: 2, stride: 2, name: "pool1"); + var conv2 = conv_layer(pool1, filter_size2, num_filters2, stride2, name: "conv2"); + var pool2 = max_pool(conv2, ksize: 2, stride: 2, name: "pool2"); + var layer_flat = flatten_layer(pool2); + var fc1 = fc_layer(layer_flat, h1, "FC1", use_relu: true); + var output_logits = fc_layer(fc1, n_classes, "OUT", use_relu: false); + + with(tf.variable_scope("Train"), delegate + { + with(tf.variable_scope("Loss"), delegate + { + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels: y, logits: output_logits), name: "loss"); + }); + + with(tf.variable_scope("Optimizer"), delegate + { + optimizer = tf.train.AdamOptimizer(learning_rate: learning_rate, name: "Adam-op").minimize(loss); + }); + + with(tf.variable_scope("Accuracy"), delegate + { + var correct_prediction = tf.equal(tf.argmax(output_logits, 1), tf.argmax(y, 1), name: "correct_pred"); + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name: "accuracy"); + }); + + with(tf.variable_scope("Prediction"), delegate + { + cls_prediction = tf.argmax(output_logits, axis: 1, name: "predictions"); + }); + }); + + return graph; + } + + /// + /// Create a 2D convolution layer + /// + /// input from previous layer + /// size of each filter + /// number of filters(or output feature maps) + /// filter stride + /// layer name + /// The output array + private Tensor conv_layer(Tensor x, int filter_size, int num_filters, int stride, string name) + { + return with(tf.variable_scope(name), delegate { + + var num_in_channel = x.shape[x.NDims - 1]; + var shape = new[] { filter_size, filter_size, num_in_channel, num_filters }; + var W = weight_variable("W", shape); + // var tf.summary.histogram("weight", W); + var b = bias_variable("b", new[] { num_filters }); + // tf.summary.histogram("bias", b); + var layer = tf.nn.conv2d(x, W, + strides: new[] { 1, stride, stride, 1 }, + padding: "SAME"); + layer += b; + return tf.nn.relu(layer); + }); + } + + /// + /// Create a max pooling layer + /// + /// input to max-pooling layer + /// size of the max-pooling filter + /// stride of the max-pooling filter + /// layer name + /// The output array + private Tensor max_pool(Tensor x, int ksize, int stride, string name) + { + return tf.nn.max_pool(x, + ksize: new[] { 1, ksize, ksize, 1 }, + strides: new[] { 1, stride, stride, 1 }, + padding: "SAME", + name: name); + } + + /// + /// Flattens the output of the convolutional layer to be fed into fully-connected layer + /// + /// input array + /// flattened array + private Tensor flatten_layer(Tensor layer) + { + return with(tf.variable_scope("Flatten_layer"), delegate + { + var layer_shape = layer.TensorShape; + var num_features = layer_shape[new Slice(1, 4)].Size; + var layer_flat = tf.reshape(layer, new[] { -1, num_features }); + + return layer_flat; + }); + } + + private RefVariable weight_variable(string name, int[] shape) + { + var initer = tf.truncated_normal_initializer(stddev: 0.01f); + return tf.get_variable(name, + dtype: tf.float32, + shape: shape, + initializer: initer); + } + + /// + /// Create a bias variable with appropriate initialization + /// + /// + /// + /// + private RefVariable bias_variable(string name, int[] shape) + { + var initial = tf.constant(0f, shape: shape, dtype: tf.float32); + return tf.get_variable(name, + dtype: tf.float32, + initializer: initial); + } + + private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = true) + { + return with(tf.variable_scope(name), delegate + { + var in_dim = x.shape[1]; + + var W = weight_variable("W_" + name, shape: new[] { in_dim, num_units }); + var b = bias_variable("b_" + name, new[] { num_units }); + + var layer = tf.matmul(x, W) + b; + if (use_relu) + layer = tf.nn.relu(layer); + + return layer; + }); + } + + public Graph ImportGraph() => throw new NotImplementedException(); + + public void Predict(Session sess) => throw new NotImplementedException(); + + public void PrepareData() + { + mnist = MNIST.read_data_sets("mnist", one_hot: true); + print("Size of:"); + print($"- Training-set:\t\t{len(mnist.train.data)}"); + print($"- Validation-set:\t{len(mnist.validation.data)}"); + } + + public void Train(Session sess) + { + // Number of training iterations in each epoch + var num_tr_iter = mnist.train.labels.len / batch_size; + + var init = tf.global_variables_initializer(); + sess.run(init); + + float loss_val = 100.0f; + float accuracy_val = 0f; + + foreach (var epoch in range(epochs)) + { + print($"Training epoch: {epoch + 1}"); + // Randomly shuffle the training data at the beginning of each epoch + var (x_train, y_train) = mnist.Randomize(mnist.train.data, mnist.train.labels); + + foreach (var iteration in range(num_tr_iter)) + { + var start = iteration * batch_size; + var end = (iteration + 1) * batch_size; + var (x_batch, y_batch) = mnist.GetNextBatch(x_train, y_train, start, end); + + // Run optimization op (backprop) + sess.run(optimizer, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + + if (iteration % display_freq == 0) + { + // Calculate and display the batch loss and accuracy + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + loss_val = result[0]; + accuracy_val = result[1]; + print($"iter {iteration.ToString("000")}: Loss={loss_val.ToString("0.0000")}, Training Accuracy={accuracy_val.ToString("P")}"); + } + } + + // Run validation after every epoch + var results1 = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.validation.data), new FeedItem(y, mnist.validation.labels)); + loss_val = results1[0]; + accuracy_val = results1[1]; + print("---------------------------------------------------------"); + print($"Epoch: {epoch + 1}, validation loss: {loss_val.ToString("0.0000")}, validation accuracy: {accuracy_val.ToString("P")}"); + print("---------------------------------------------------------"); + } + } + + public void Test(Session sess) + { + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels)); + loss_test = result[0]; + accuracy_test = result[1]; + print("---------------------------------------------------------"); + print($"Test loss: {loss_test.ToString("0.0000")}, test accuracy: {accuracy_test.ToString("P")}"); + print("---------------------------------------------------------"); + } + } +} diff --git a/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs b/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs new file mode 100644 index 00000000..a719a2d7 --- /dev/null +++ b/test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs @@ -0,0 +1,208 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; +using TensorFlowNET.Examples.Utility; +using static Tensorflow.Python; + +namespace TensorFlowNET.Examples.ImageProcess +{ + /// + /// Neural Network classifier for Hand Written Digits + /// Sample Neural Network architecture with two layers implemented for classifying MNIST digits. + /// Use Stochastic Gradient Descent (SGD) optimizer. + /// http://www.easy-tensorflow.com/tf-tutorials/neural-networks + /// + public class DigitRecognitionNN : IExample + { + public bool Enabled { get; set; } = true; + public bool IsImportingGraph { get; set; } = false; + + public string Name => "Digits Recognition Neural Network"; + + const int img_h = 28; + const int img_w = 28; + int img_size_flat = img_h * img_w; // 784, the total number of pixels + int n_classes = 10; // Number of classes, one class per digit + // Hyper-parameters + int epochs = 10; + int batch_size = 100; + float learning_rate = 0.001f; + int h1 = 200; // number of nodes in the 1st hidden layer + Datasets mnist; + + Tensor x, y; + Tensor loss, accuracy; + Operation optimizer; + + int display_freq = 100; + float accuracy_test = 0f; + float loss_test = 1f; + + public bool Run() + { + PrepareData(); + BuildGraph(); + + with(tf.Session(), sess => + { + Train(sess); + Test(sess); + }); + + return loss_test < 0.09 && accuracy_test > 0.95; + } + + public Graph BuildGraph() + { + var graph = new Graph().as_default(); + + // Placeholders for inputs (x) and outputs(y) + x = tf.placeholder(tf.float32, shape: (-1, img_size_flat), name: "X"); + y = tf.placeholder(tf.float32, shape: (-1, n_classes), name: "Y"); + + // Create a fully-connected layer with h1 nodes as hidden layer + var fc1 = fc_layer(x, h1, "FC1", use_relu: true); + // Create a fully-connected layer with n_classes nodes as output layer + var output_logits = fc_layer(fc1, n_classes, "OUT", use_relu: false); + // Define the loss function, optimizer, and accuracy + var logits = tf.nn.softmax_cross_entropy_with_logits(labels: y, logits: output_logits); + loss = tf.reduce_mean(logits, name: "loss"); + optimizer = tf.train.AdamOptimizer(learning_rate: learning_rate, name: "Adam-op").minimize(loss); + var correct_prediction = tf.equal(tf.argmax(output_logits, 1), tf.argmax(y, 1), name: "correct_pred"); + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name: "accuracy"); + + // Network predictions + var cls_prediction = tf.argmax(output_logits, axis: 1, name: "predictions"); + + return graph; + } + + private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = true) + { + var in_dim = x.shape[1]; + + var initer = tf.truncated_normal_initializer(stddev: 0.01f); + var W = tf.get_variable("W_" + name, + dtype: tf.float32, + shape: (in_dim, num_units), + initializer: initer); + + var initial = tf.constant(0f, num_units); + var b = tf.get_variable("b_" + name, + dtype: tf.float32, + initializer: initial); + + var layer = tf.matmul(x, W) + b; + if (use_relu) + layer = tf.nn.relu(layer); + + return layer; + } + + public Graph ImportGraph() => throw new NotImplementedException(); + + public void Predict(Session sess) => throw new NotImplementedException(); + + public void PrepareData() + { + mnist = MNIST.read_data_sets("mnist", one_hot: true); + } + + public void Train(Session sess) + { + // Number of training iterations in each epoch + var num_tr_iter = mnist.train.labels.len / batch_size; + + var init = tf.global_variables_initializer(); + sess.run(init); + + float loss_val = 100.0f; + float accuracy_val = 0f; + + foreach (var epoch in range(epochs)) + { + print($"Training epoch: {epoch + 1}"); + // Randomly shuffle the training data at the beginning of each epoch + var (x_train, y_train) = randomize(mnist.train.data, mnist.train.labels); + + foreach (var iteration in range(num_tr_iter)) + { + var start = iteration * batch_size; + var end = (iteration + 1) * batch_size; + var (x_batch, y_batch) = get_next_batch(x_train, y_train, start, end); + + // Run optimization op (backprop) + sess.run(optimizer, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + + if (iteration % display_freq == 0) + { + // Calculate and display the batch loss and accuracy + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, x_batch), new FeedItem(y, y_batch)); + loss_val = result[0]; + accuracy_val = result[1]; + print($"iter {iteration.ToString("000")}: Loss={loss_val.ToString("0.0000")}, Training Accuracy={accuracy_val.ToString("P")}"); + } + } + + // Run validation after every epoch + var results1 = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.validation.data), new FeedItem(y, mnist.validation.labels)); + loss_val = results1[0]; + accuracy_val = results1[1]; + print("---------------------------------------------------------"); + print($"Epoch: {epoch + 1}, validation loss: {loss_val.ToString("0.0000")}, validation accuracy: {accuracy_val.ToString("P")}"); + print("---------------------------------------------------------"); + } + } + + public void Test(Session sess) + { + var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels)); + loss_test = result[0]; + accuracy_test = result[1]; + print("---------------------------------------------------------"); + print($"Test loss: {loss_test.ToString("0.0000")}, test accuracy: {accuracy_test.ToString("P")}"); + print("---------------------------------------------------------"); + } + + private (NDArray, NDArray) randomize(NDArray x, NDArray y) + { + var perm = np.random.permutation(y.shape[0]); + + np.random.shuffle(perm); + return (mnist.train.data[perm], mnist.train.labels[perm]); + } + + /// + /// selects a few number of images determined by the batch_size variable (if you don't know why, read about Stochastic Gradient Method) + /// + /// + /// + /// + /// + /// + private (NDArray, NDArray) get_next_batch(NDArray x, NDArray y, int start, int end) + { + var x_batch = x[$"{start}:{end}"]; + var y_batch = y[$"{start}:{end}"]; + return (x_batch, y_batch); + } + } +} diff --git a/test/TensorFlowNET.Examples/ImageProcess/ImageBackgroundRemoval.cs b/test/TensorFlowNET.Examples/ImageProcess/ImageBackgroundRemoval.cs index 29e69a8d..12fd526f 100644 --- a/test/TensorFlowNET.Examples/ImageProcess/ImageBackgroundRemoval.cs +++ b/test/TensorFlowNET.Examples/ImageProcess/ImageBackgroundRemoval.cs @@ -68,12 +68,17 @@ namespace TensorFlowNET.Examples.ImageProcess throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/ImageProcess/ImageRecognitionInception.cs b/test/TensorFlowNET.Examples/ImageProcess/ImageRecognitionInception.cs index b96bf494..ec6e06e7 100644 --- a/test/TensorFlowNET.Examples/ImageProcess/ImageRecognitionInception.cs +++ b/test/TensorFlowNET.Examples/ImageProcess/ImageRecognitionInception.cs @@ -125,12 +125,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/ImageProcess/InceptionArchGoogLeNet.cs b/test/TensorFlowNET.Examples/ImageProcess/InceptionArchGoogLeNet.cs index 63a69425..985d855d 100644 --- a/test/TensorFlowNET.Examples/ImageProcess/InceptionArchGoogLeNet.cs +++ b/test/TensorFlowNET.Examples/ImageProcess/InceptionArchGoogLeNet.cs @@ -118,12 +118,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/ImageProcess/ObjectDetection.cs b/test/TensorFlowNET.Examples/ImageProcess/ObjectDetection.cs index 485c5b30..32f7841b 100644 --- a/test/TensorFlowNET.Examples/ImageProcess/ObjectDetection.cs +++ b/test/TensorFlowNET.Examples/ImageProcess/ObjectDetection.cs @@ -1,4 +1,19 @@ -using Newtonsoft.Json; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + using NumSharp; using System; using System.Collections.Generic; @@ -13,7 +28,6 @@ using static Tensorflow.Python; namespace TensorFlowNET.Examples { - public class ObjectDetection : IExample { public bool Enabled { get; set; } = true; @@ -155,12 +169,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs b/test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs index 3793027a..0e7d8961 100644 --- a/test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs +++ b/test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs @@ -1,4 +1,20 @@ -using Google.Protobuf; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Google.Protobuf; using NumSharp; using System; using System.Collections.Generic; @@ -264,12 +280,12 @@ namespace TensorFlowNET.Examples.ImageProcess private (Operation, Tensor, Tensor, Tensor, Tensor) add_final_retrain_ops(int class_count, string final_tensor_name, Tensor bottleneck_tensor, bool quantize_layer, bool is_training) { - var (batch_size, bottleneck_tensor_size) = (bottleneck_tensor.GetShape().Dimensions[0], bottleneck_tensor.GetShape().Dimensions[1]); + var (batch_size, bottleneck_tensor_size) = (bottleneck_tensor.TensorShape.Dimensions[0], bottleneck_tensor.TensorShape.Dimensions[1]); with(tf.name_scope("input"), scope => { bottleneck_input = tf.placeholder_with_default( bottleneck_tensor, - shape: bottleneck_tensor.GetShape().Dimensions, + shape: bottleneck_tensor.TensorShape.Dimensions, name: "BottleneckInputPlaceholder"); ground_truth_input = tf.placeholder(tf.int64, new TensorShape(batch_size), name: "GroundTruthInput"); @@ -681,12 +697,17 @@ namespace TensorFlowNET.Examples.ImageProcess throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) + { + throw new NotImplementedException(); + } + + public void Predict(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/Program.cs b/test/TensorFlowNET.Examples/Program.cs index 49192704..1e001b0c 100644 --- a/test/TensorFlowNET.Examples/Program.cs +++ b/test/TensorFlowNET.Examples/Program.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; diff --git a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj index 470399ff..e64e6df8 100644 --- a/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj +++ b/test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj @@ -17,7 +17,6 @@ - diff --git a/test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs b/test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs index 99ac6d94..5f81e12d 100644 --- a/test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs +++ b/test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs @@ -148,12 +148,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/CnnTextClassification.cs b/test/TensorFlowNET.Examples/TextProcess/CnnTextClassification.cs index 883783b2..cf52618e 100644 --- a/test/TensorFlowNET.Examples/TextProcess/CnnTextClassification.cs +++ b/test/TensorFlowNET.Examples/TextProcess/CnnTextClassification.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; @@ -9,6 +25,7 @@ using Newtonsoft.Json; using NumSharp; using Tensorflow; using Tensorflow.Sessions; +using TensorFlowNET.Examples.Text; using TensorFlowNET.Examples.Utility; using static Tensorflow.Python; @@ -22,29 +39,37 @@ namespace TensorFlowNET.Examples public bool Enabled { get; set; } = true; public string Name => "CNN Text Classification"; public int? DataLimit = null; - public bool IsImportingGraph { get; set; } = true; + public bool IsImportingGraph { get; set; } = false; - private const string dataDir = "word_cnn"; - private string dataFileName = "dbpedia_csv.tar.gz"; + const string dataDir = "cnn_text"; + string dataFileName = "dbpedia_csv.tar.gz"; - private const string TRAIN_PATH = "word_cnn/dbpedia_csv/train.csv"; - private const string TEST_PATH = "word_cnn/dbpedia_csv/test.csv"; + string TRAIN_PATH = $"{dataDir}/dbpedia_csv/train.csv"; + string TEST_PATH = $"{dataDir}/dbpedia_csv/test.csv"; - private const int NUM_CLASS = 14; - private const int BATCH_SIZE = 64; - private const int NUM_EPOCHS = 10; - private const int WORD_MAX_LEN = 100; - private const int CHAR_MAX_LEN = 1014; + int NUM_CLASS = 14; + int BATCH_SIZE = 64; + int NUM_EPOCHS = 10; + int WORD_MAX_LEN = 100; + int CHAR_MAX_LEN = 1014; - protected float loss_value = 0; - int vocabulary_size = 50000; + float loss_value = 0; + double max_accuracy = 0; + + int alphabet_size = -1; + int vocabulary_size = -1; NDArray train_x, valid_x, train_y, valid_y; + ITextModel textModel; + public string ModelName = "word_cnn"; // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn + public bool Run() { PrepareData(); + var graph = IsImportingGraph ? ImportGraph() : BuildGraph(); + with(tf.Session(graph), sess => Train(sess)); - return Train(); + return max_accuracy > 0.9; } // TODO: this originally is an SKLearn utility function. it randomizes train and test which we don't do here @@ -61,15 +86,10 @@ namespace TensorFlowNET.Examples valid_y = y[new Slice(start: train_size)]; Console.WriteLine("\tDONE"); - train_x = np.Load(Path.Join("word_cnn", "train_x.npy")); - valid_x = np.Load(Path.Join("word_cnn", "valid_x.npy")); - train_y = np.Load(Path.Join("word_cnn", "train_y.npy")); - valid_y = np.Load(Path.Join("word_cnn", "valid_y.npy")); - return (train_x, valid_x, train_y, valid_y); } - private static void FillWithShuffledLabels(int[][] x, int[] y, int[][] shuffled_x, int[] shuffled_y, Random random, Dictionary> labels) + private void FillWithShuffledLabels(int[][] x, int[] y, int[][] shuffled_x, int[] shuffled_y, Random random, Dictionary> labels) { int i = 0; var label_keys = labels.Keys.ToArray(); @@ -114,12 +134,18 @@ namespace TensorFlowNET.Examples Compress.UnZip(Path.Combine(dataDir, "dbpedia_subset.zip"), Path.Combine(dataDir, "dbpedia_csv")); Console.WriteLine("Building dataset..."); + var (x, y) = (new int[0][], new int[0]); - int alphabet_size = 0; - - var word_dict = DataHelpers.build_word_dict(TRAIN_PATH); - //vocabulary_size = len(word_dict); - var (x, y) = DataHelpers.build_word_dataset(TRAIN_PATH, word_dict, WORD_MAX_LEN); + if(ModelName == "char_cnn") + { + (x, y, alphabet_size) = DataHelpers.build_char_dataset(TRAIN_PATH, "char_cnn", CHAR_MAX_LEN); + } + else + { + var word_dict = DataHelpers.build_word_dict(TRAIN_PATH); + vocabulary_size = len(word_dict); + (x, y) = DataHelpers.build_word_dataset(TRAIN_PATH, word_dict, WORD_MAX_LEN); + } Console.WriteLine("\tDONE "); @@ -156,83 +182,22 @@ namespace TensorFlowNET.Examples { var graph = tf.Graph().as_default(); - var embedding_size = 128; - var learning_rate = 0.001f; - var filter_sizes = new int[3, 4, 5]; - var num_filters = 100; - var document_max_len = 100; - - var x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x"); - var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y"); - var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training"); - var global_step = tf.Variable(0, trainable: false); - var keep_prob = tf.where(is_training, 0.5f, 1.0f); - Tensor x_emb = null; - - with(tf.name_scope("embedding"), scope => + switch (ModelName) { - var init_embeddings = tf.random_uniform(new int[] { vocabulary_size, embedding_size }); - var embeddings = tf.get_variable("embeddings", initializer: init_embeddings); - x_emb = tf.nn.embedding_lookup(embeddings, x); - x_emb = tf.expand_dims(x_emb, -1); - }); - - var pooled_outputs = new List(); - for (int len = 0; len < filter_sizes.Rank; len++) - { - int filter_size = filter_sizes.GetLength(len); - var conv = tf.layers.conv2d( - x_emb, - filters: num_filters, - kernel_size: new int[] { filter_size, embedding_size }, - strides: new int[] { 1, 1 }, - padding: "VALID", - activation: tf.nn.relu()); - - var pool = tf.layers.max_pooling2d( - conv, - pool_size: new[] { document_max_len - filter_size + 1, 1 }, - strides: new[] { 1, 1 }, - padding: "VALID"); - - pooled_outputs.Add(pool); + case "word_cnn": + textModel = new WordCnn(vocabulary_size, WORD_MAX_LEN, NUM_CLASS); + break; + case "char_cnn": + textModel = new CharCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS); + break; } - var h_pool = tf.concat(pooled_outputs, 3); - var h_pool_flat = tf.reshape(h_pool, new TensorShape(-1, num_filters * filter_sizes.Rank)); - Tensor h_drop = null; - with(tf.name_scope("dropout"), delegate - { - h_drop = tf.nn.dropout(h_pool_flat, keep_prob); - }); - - Tensor logits = null; - Tensor predictions = null; - with(tf.name_scope("output"), delegate - { - logits = tf.layers.dense(h_drop, NUM_CLASS); - predictions = tf.argmax(logits, -1, output_type: tf.int32); - }); - - with(tf.name_scope("loss"), delegate - { - var sscel = tf.nn.sparse_softmax_cross_entropy_with_logits(logits: logits, labels: y); - var loss = tf.reduce_mean(sscel); - var adam = tf.train.AdamOptimizer(learning_rate); - var optimizer = adam.minimize(loss, global_step: global_step); - }); - - with(tf.name_scope("accuracy"), delegate - { - var correct_predictions = tf.equal(predictions, y); - var accuracy = tf.reduce_mean(tf.cast(correct_predictions, TF_DataType.TF_FLOAT), name: "accuracy"); - }); - return graph; } - private bool Train(Session sess, Graph graph) + public void Train(Session sess) { + var graph = tf.get_default_graph(); var stopwatch = Stopwatch.StartNew(); sess.run(tf.global_variables_initializer()); @@ -240,7 +205,6 @@ namespace TensorFlowNET.Examples var train_batches = batch_iter(train_x, train_y, BATCH_SIZE, NUM_EPOCHS); var num_batches_per_epoch = (len(train_x) - 1) / BATCH_SIZE + 1; - double max_accuracy = 0; Tensor is_training = graph.OperationByName("is_training"); Tensor model_x = graph.OperationByName("x"); @@ -265,10 +229,7 @@ namespace TensorFlowNET.Examples loss_value = result[2]; var step = (int)result[1]; if (step % 10 == 0) - { - var estimate = TimeSpan.FromSeconds((stopwatch.Elapsed.TotalSeconds / i) * total); - Console.WriteLine($"Training on batch {i}/{total} loss: {loss_value}. Estimated training time: {estimate}"); - } + Console.WriteLine($"Training on batch {i}/{total} loss: {loss_value.ToString("0.0000")}."); if (step % 100 == 0) { @@ -291,7 +252,7 @@ namespace TensorFlowNET.Examples var valid_accuracy = sum_accuracy / cnt; - print($"\nValidation Accuracy = {valid_accuracy}\n"); + print($"\nValidation Accuracy = {valid_accuracy.ToString("P")}\n"); // Save model if (valid_accuracy > max_accuracy) @@ -302,18 +263,14 @@ namespace TensorFlowNET.Examples } } } - - return max_accuracy > 0.8; } - public bool Train() + public void Predict(Session sess) { - var graph = IsImportingGraph ? ImportGraph() : BuildGraph(); - // string json = JsonConvert.SerializeObject(graph, Formatting.Indented); - return with(tf.Session(graph), sess => Train(sess, graph)); + throw new NotImplementedException(); } - public bool Predict() + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs b/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs index 8b5f79e2..b8e41640 100644 --- a/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs +++ b/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs @@ -55,8 +55,6 @@ namespace TensorFlowNET.Examples public static (int[][], int[], int) build_char_dataset(string path, string model, int document_max_len, int? limit = null, bool shuffle=true) { - if (model != "vd_cnn") - throw new NotImplementedException(model); string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:’'\"/|_#$%ˆ&*˜‘+=<>()[]{} "; /*if (step == "train") df = pd.read_csv(TRAIN_PATH, names =["class", "title", "content"]);*/ diff --git a/test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs b/test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs index 26626c1a..cb3aa30e 100644 --- a/test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs +++ b/test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs @@ -44,12 +44,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs b/test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs index 1fbfddb6..bb23c664 100644 --- a/test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs +++ b/test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs @@ -40,12 +40,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs b/test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs index 1aa1e44a..0cf75d2e 100644 --- a/test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs +++ b/test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs @@ -217,12 +217,17 @@ namespace TensorFlowNET.Examples.Text.NER throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs b/test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs index 33bee661..10615e03 100644 --- a/test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs +++ b/test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs @@ -16,7 +16,7 @@ namespace TensorFlowNET.Examples public bool IsImportingGraph { get; set; } = false; - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } @@ -41,7 +41,12 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs b/test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs deleted file mode 100644 index 42841664..00000000 --- a/test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs +++ /dev/null @@ -1,293 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Text; -using NumSharp; -using Tensorflow; -using Tensorflow.Keras.Engine; -using Tensorflow.Sessions; -using TensorFlowNET.Examples.Text.cnn_models; -using TensorFlowNET.Examples.TextClassification; -using TensorFlowNET.Examples.Utility; -using static Tensorflow.Python; - -namespace TensorFlowNET.Examples -{ - /// - /// https://github.com/dongjun-Lee/text-classification-models-tf - /// - public class TextClassificationTrain : IExample - { - public bool Enabled { get; set; } = false; - public string Name => "Text Classification"; - public int? DataLimit = null; - public bool IsImportingGraph { get; set; } = true; - public bool UseSubset = false; // <----- set this true to use a limited subset of dbpedia - - private string dataDir = "text_classification"; - private string dataFileName = "dbpedia_csv.tar.gz"; - - public string model_name = "word_cnn"; // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn - - private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv"; - private const string SUBSET_PATH = "text_classification/dbpedia_csv/dbpedia_6400.csv"; - private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv"; - - private const int NUM_CLASS = 14; - private const int BATCH_SIZE = 64; - private const int NUM_EPOCHS = 10; - private const int WORD_MAX_LEN = 100; - private const int CHAR_MAX_LEN = 1014; - - protected float loss_value = 0; - - public bool Run() - { - PrepareData(); - var graph = tf.Graph().as_default(); - return with(tf.Session(graph), sess => - { - if (IsImportingGraph) - return RunWithImportedGraph(sess, graph); - else - return RunWithBuiltGraph(sess, graph); - }); - } - - protected virtual bool RunWithImportedGraph(Session sess, Graph graph) - { - var stopwatch = Stopwatch.StartNew(); - Console.WriteLine("Building dataset..."); - var path = UseSubset ? SUBSET_PATH : TRAIN_PATH; - int[][] x = null; - int[] y = null; - int alphabet_size = 0; - int vocabulary_size = 0; - - if (model_name == "vd_cnn") - (x, y, alphabet_size) = DataHelpers.build_char_dataset(path, model_name, CHAR_MAX_LEN, DataLimit = null, shuffle:!UseSubset); - else - { - var word_dict = DataHelpers.build_word_dict(TRAIN_PATH); - vocabulary_size = len(word_dict); - (x, y) = DataHelpers.build_word_dataset(TRAIN_PATH, word_dict, WORD_MAX_LEN); - } - - Console.WriteLine("\tDONE "); - - var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f); - Console.WriteLine("Training set size: " + train_x.len); - Console.WriteLine("Test set size: " + valid_x.len); - - Console.WriteLine("Import graph..."); - var meta_file = model_name + ".meta"; - tf.train.import_meta_graph(Path.Join("graph", meta_file)); - Console.WriteLine("\tDONE " + stopwatch.Elapsed); - - sess.run(tf.global_variables_initializer()); - var saver = tf.train.Saver(tf.global_variables()); - - var train_batches = batch_iter(train_x, train_y, BATCH_SIZE, NUM_EPOCHS); - var num_batches_per_epoch = (len(train_x) - 1) / BATCH_SIZE + 1; - double max_accuracy = 0; - - Tensor is_training = graph.OperationByName("is_training"); - Tensor model_x = graph.OperationByName("x"); - Tensor model_y = graph.OperationByName("y"); - Tensor loss = graph.OperationByName("loss/Mean"); // word_cnn - Operation optimizer = graph.OperationByName("loss/Adam"); // word_cnn - Tensor global_step = graph.OperationByName("Variable"); - Tensor accuracy = graph.OperationByName("accuracy/accuracy"); - stopwatch = Stopwatch.StartNew(); - int i = 0; - foreach (var (x_batch, y_batch, total) in train_batches) - { - i++; - var train_feed_dict = new FeedDict - { - [model_x] = x_batch, - [model_y] = y_batch, - [is_training] = true, - }; - //Console.WriteLine("x: " + x_batch.ToString() + "\n"); - //Console.WriteLine("y: " + y_batch.ToString()); - // original python: - //_, step, loss = sess.run([model.optimizer, model.global_step, model.loss], feed_dict = train_feed_dict) - var result = sess.run(new ITensorOrOperation[] { optimizer, global_step, loss }, train_feed_dict); - loss_value = result[2]; - var step = (int)result[1]; - if (step % 10 == 0) - { - var estimate = TimeSpan.FromSeconds((stopwatch.Elapsed.TotalSeconds / i) * total); - Console.WriteLine($"Training on batch {i}/{total} loss: {loss_value}. Estimated training time: {estimate}"); - } - - if (step % 100 == 0) - { - // # Test accuracy with validation data for each epoch. - var valid_batches = batch_iter(valid_x, valid_y, BATCH_SIZE, 1); - var (sum_accuracy, cnt) = (0.0f, 0); - foreach (var (valid_x_batch, valid_y_batch, total_validation_batches) in valid_batches) - { - var valid_feed_dict = new FeedDict - { - [model_x] = valid_x_batch, - [model_y] = valid_y_batch, - [is_training] = false - }; - var result1 = sess.run(accuracy, valid_feed_dict); - float accuracy_value = result1; - sum_accuracy += accuracy_value; - cnt += 1; - } - - var valid_accuracy = sum_accuracy / cnt; - - print($"\nValidation Accuracy = {valid_accuracy}\n"); - - // # Save model - if (valid_accuracy > max_accuracy) - { - max_accuracy = valid_accuracy; - // saver.save(sess, $"{dataDir}/{model_name}.ckpt", global_step: step.ToString()); - print("Model is saved.\n"); - } - } - } - - return false; - } - - protected virtual bool RunWithBuiltGraph(Session session, Graph graph) - { - Console.WriteLine("Building dataset..."); - var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", model_name, CHAR_MAX_LEN, DataLimit); - - var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f); - - ITextClassificationModel model = null; - switch (model_name) // word_cnn | char_cnn | vd_cnn | word_rnn | att_rnn | rcnn - { - case "word_cnn": - case "char_cnn": - case "word_rnn": - case "att_rnn": - case "rcnn": - throw new NotImplementedException(); - break; - case "vd_cnn": - model = new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS); - break; - } - // todo train the model - return false; - } - - // TODO: this originally is an SKLearn utility function. it randomizes train and test which we don't do here - private (NDArray, NDArray, NDArray, NDArray) train_test_split(NDArray x, NDArray y, float test_size = 0.3f) - { - Console.WriteLine("Splitting in Training and Testing data..."); - int len = x.shape[0]; - //int classes = y.Data().Distinct().Count(); - //int samples = len / classes; - int train_size = (int)Math.Round(len * (1 - test_size)); - var train_x = x[new Slice(stop: train_size), new Slice()]; - var valid_x = x[new Slice(start: train_size), new Slice()]; - var train_y = y[new Slice(stop: train_size)]; - var valid_y = y[new Slice(start: train_size)]; - Console.WriteLine("\tDONE"); - return (train_x, valid_x, train_y, valid_y); - } - - private static void FillWithShuffledLabels(int[][] x, int[] y, int[][] shuffled_x, int[] shuffled_y, Random random, Dictionary> labels) - { - int i = 0; - var label_keys = labels.Keys.ToArray(); - while (i < shuffled_x.Length) - { - var key = label_keys[random.Next(label_keys.Length)]; - var set = labels[key]; - var index = set.First(); - if (set.Count == 0) - { - labels.Remove(key); // remove the set as it is empty - label_keys = labels.Keys.ToArray(); - } - shuffled_x[i] = x[index]; - shuffled_y[i] = y[index]; - i++; - } - } - - private IEnumerable<(NDArray, NDArray, int)> batch_iter(NDArray inputs, NDArray outputs, int batch_size, int num_epochs) - { - var num_batches_per_epoch = (len(inputs) - 1) / batch_size + 1; - var total_batches = num_batches_per_epoch * num_epochs; - foreach (var epoch in range(num_epochs)) - { - foreach (var batch_num in range(num_batches_per_epoch)) - { - var start_index = batch_num * batch_size; - var end_index = Math.Min((batch_num + 1) * batch_size, len(inputs)); - if (end_index <= start_index) - break; - yield return (inputs[new Slice(start_index, end_index)], outputs[new Slice(start_index, end_index)], total_batches); - } - } - } - - public void PrepareData() - { - if (UseSubset) - { - var url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/dbpedia_subset.zip"; - Web.Download(url, dataDir, "dbpedia_subset.zip"); - Compress.UnZip(Path.Combine(dataDir, "dbpedia_subset.zip"), Path.Combine(dataDir, "dbpedia_csv")); - } - else - { - string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz"; - Web.Download(url, dataDir, dataFileName); - Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir); - } - - if (IsImportingGraph) - { - // download graph meta data - var meta_file = model_name + ".meta"; - var meta_path = Path.Combine("graph", meta_file); - if (File.GetLastWriteTime(meta_path) < new DateTime(2019, 05, 11)) - { - // delete old cached file which contains errors - Console.WriteLine("Discarding cached file: " + meta_path); - File.Delete(meta_path); - } - var url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/" + meta_file; - Web.Download(url, "graph", meta_file); - } - } - - public Graph ImportGraph() - { - throw new NotImplementedException(); - } - - public Graph BuildGraph() - { - throw new NotImplementedException(); - } - - public bool Train() - { - throw new NotImplementedException(); - } - - public bool Predict() - { - throw new NotImplementedException(); - } - } -} diff --git a/test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs b/test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs index dc3f27f3..4884a76a 100644 --- a/test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs +++ b/test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs @@ -214,12 +214,17 @@ namespace TensorFlowNET.Examples throw new NotImplementedException(); } - public bool Train() + public void Train(Session sess) { throw new NotImplementedException(); } - public bool Predict() + public void Predict(Session sess) + { + throw new NotImplementedException(); + } + + public void Test(Session sess) { throw new NotImplementedException(); } diff --git a/test/TensorFlowNET.Examples/TextProcess/cnn_models/CharCnn.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/CharCnn.cs new file mode 100644 index 00000000..43097b5b --- /dev/null +++ b/test/TensorFlowNET.Examples/TextProcess/cnn_models/CharCnn.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tensorflow; +using static Tensorflow.Python; + +namespace TensorFlowNET.Examples.Text +{ + public class CharCnn : ITextModel + { + public CharCnn(int alphabet_size, int document_max_len, int num_class) + { + var learning_rate = 0.001f; + var filter_sizes = new int[] { 7, 7, 3, 3, 3, 3 }; + var num_filters = 256; + var kernel_initializer = tf.truncated_normal_initializer(stddev: 0.05f); + + var x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x"); + var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y"); + var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training"); + var global_step = tf.Variable(0, trainable: false); + var keep_prob = tf.where(is_training, 0.5f, 1.0f); + + var x_one_hot = tf.one_hot(x, alphabet_size); + var x_expanded = tf.expand_dims(x_one_hot, -1); + + // ============= Convolutional Layers ============= + Tensor pool1 = null, pool2 = null; + Tensor conv3 = null, conv4 = null, conv5 = null, conv6 = null; + Tensor h_pool = null; + + with(tf.name_scope("conv-maxpool-1"), delegate + { + var conv1 = tf.layers.conv2d(x_expanded, + filters: num_filters, + kernel_size: new[] { filter_sizes[0], alphabet_size }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + + pool1 = tf.layers.max_pooling2d(conv1, + pool_size: new[] { 3, 1 }, + strides: new[] { 3, 1 }); + pool1 = tf.transpose(pool1, new[] { 0, 1, 3, 2 }); + }); + + with(tf.name_scope("conv-maxpool-2"), delegate + { + var conv2 = tf.layers.conv2d(pool1, + filters: num_filters, + kernel_size: new[] {filter_sizes[1], num_filters }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + + pool2 = tf.layers.max_pooling2d(conv2, + pool_size: new[] { 3, 1 }, + strides: new[] { 3, 1 }); + pool2 = tf.transpose(pool2, new[] { 0, 1, 3, 2 }); + }); + + with(tf.name_scope("conv-3"), delegate + { + conv3 = tf.layers.conv2d(pool2, + filters: num_filters, + kernel_size: new[] { filter_sizes[2], num_filters }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + conv3 = tf.transpose(conv3, new[] { 0, 1, 3, 2 }); + }); + + with(tf.name_scope("conv-4"), delegate + { + conv4 = tf.layers.conv2d(conv3, + filters: num_filters, + kernel_size: new[] { filter_sizes[3], num_filters }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + conv4 = tf.transpose(conv4, new[] { 0, 1, 3, 2 }); + }); + + with(tf.name_scope("conv-5"), delegate + { + conv5 = tf.layers.conv2d(conv4, + filters: num_filters, + kernel_size: new[] { filter_sizes[4], num_filters }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + conv5 = tf.transpose(conv5, new[] { 0, 1, 3, 2 }); + }); + + with(tf.name_scope("conv-maxpool-6"), delegate + { + conv6 = tf.layers.conv2d(conv5, + filters: num_filters, + kernel_size: new[] { filter_sizes[5], num_filters }, + kernel_initializer: kernel_initializer, + activation: tf.nn.relu()); + + var pool6 = tf.layers.max_pooling2d(conv6, + pool_size: new[] { 3, 1 }, + strides: new[] { 3, 1 }); + pool6 = tf.transpose(pool6, new[] { 0, 2, 1, 3 }); + + h_pool = tf.reshape(pool6, new[] { -1, 34 * num_filters }); + }); + + // ============= Fully Connected Layers ============= + Tensor fc1_out = null, fc2_out = null; + Tensor logits = null; + Tensor predictions = null; + + with(tf.name_scope("fc-1"), delegate + { + fc1_out = tf.layers.dense(h_pool, + 1024, + activation: tf.nn.relu(), + kernel_initializer: kernel_initializer); + }); + + with(tf.name_scope("fc-2"), delegate + { + fc2_out = tf.layers.dense(fc1_out, + 1024, + activation: tf.nn.relu(), + kernel_initializer: kernel_initializer); + }); + + with(tf.name_scope("fc-3"), delegate + { + logits = tf.layers.dense(fc2_out, + num_class, + kernel_initializer: kernel_initializer); + predictions = tf.argmax(logits, -1, output_type: tf.int32); + }); + + with(tf.name_scope("loss"), delegate + { + var y_one_hot = tf.one_hot(y, num_class); + var loss = tf.reduce_mean( + tf.nn.softmax_cross_entropy_with_logits_v2(logits: logits, labels: y_one_hot)); + var optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step: global_step); + }); + + with(tf.name_scope("accuracy"), delegate + { + var correct_predictions = tf.equal(predictions, y); + var accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name: "accuracy"); + }); + } + } +} diff --git a/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs deleted file mode 100644 index 942f2e04..00000000 --- a/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Tensorflow; - -namespace TensorFlowNET.Examples.Text.cnn_models -{ - interface ITextClassificationModel - { - Tensor is_training { get; } - Tensor x { get;} - Tensor y { get; } - } -} diff --git a/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextModel.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextModel.cs new file mode 100644 index 00000000..ab4bb372 --- /dev/null +++ b/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextModel.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.Examples.Text +{ + interface ITextModel + { + } +} diff --git a/test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs index f4f430a5..06852faf 100644 --- a/test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs +++ b/test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs @@ -3,12 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Tensorflow; -using TensorFlowNET.Examples.Text.cnn_models; using static Tensorflow.Python; -namespace TensorFlowNET.Examples.TextClassification +namespace TensorFlowNET.Examples.Text { - public class VdCnn : ITextClassificationModel + public class VdCnn : ITextModel { private int embedding_size; private int[] filter_sizes; diff --git a/test/TensorFlowNET.Examples/TextProcess/cnn_models/WordCnn.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/WordCnn.cs new file mode 100644 index 00000000..749ec333 --- /dev/null +++ b/test/TensorFlowNET.Examples/TextProcess/cnn_models/WordCnn.cs @@ -0,0 +1,102 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tensorflow; +using static Tensorflow.Python; + +namespace TensorFlowNET.Examples.Text +{ + public class WordCnn : ITextModel + { + public WordCnn(int vocabulary_size, int document_max_len, int num_class) + { + var embedding_size = 128; + var learning_rate = 0.001f; + var filter_sizes = new int[3, 4, 5]; + var num_filters = 100; + + var x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x"); + var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y"); + var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training"); + var global_step = tf.Variable(0, trainable: false); + var keep_prob = tf.where(is_training, 0.5f, 1.0f); + Tensor x_emb = null; + + with(tf.name_scope("embedding"), scope => + { + var init_embeddings = tf.random_uniform(new int[] { vocabulary_size, embedding_size }); + var embeddings = tf.get_variable("embeddings", initializer: init_embeddings); + x_emb = tf.nn.embedding_lookup(embeddings, x); + x_emb = tf.expand_dims(x_emb, -1); + }); + + var pooled_outputs = new List(); + for (int len = 0; len < filter_sizes.Rank; len++) + { + int filter_size = filter_sizes.GetLength(len); + var conv = tf.layers.conv2d( + x_emb, + filters: num_filters, + kernel_size: new int[] { filter_size, embedding_size }, + strides: new int[] { 1, 1 }, + padding: "VALID", + activation: tf.nn.relu()); + + var pool = tf.layers.max_pooling2d( + conv, + pool_size: new[] { document_max_len - filter_size + 1, 1 }, + strides: new[] { 1, 1 }, + padding: "VALID"); + + pooled_outputs.Add(pool); + } + + var h_pool = tf.concat(pooled_outputs, 3); + var h_pool_flat = tf.reshape(h_pool, new TensorShape(-1, num_filters * filter_sizes.Rank)); + Tensor h_drop = null; + with(tf.name_scope("dropout"), delegate + { + h_drop = tf.nn.dropout(h_pool_flat, keep_prob); + }); + + Tensor logits = null; + Tensor predictions = null; + with(tf.name_scope("output"), delegate + { + logits = tf.layers.dense(h_drop, num_class); + predictions = tf.argmax(logits, -1, output_type: tf.int32); + }); + + with(tf.name_scope("loss"), delegate + { + var sscel = tf.nn.sparse_softmax_cross_entropy_with_logits(logits: logits, labels: y); + var loss = tf.reduce_mean(sscel); + var adam = tf.train.AdamOptimizer(learning_rate); + var optimizer = adam.minimize(loss, global_step: global_step); + }); + + with(tf.name_scope("accuracy"), delegate + { + var correct_predictions = tf.equal(predictions, y); + var accuracy = tf.reduce_mean(tf.cast(correct_predictions, TF_DataType.TF_FLOAT), name: "accuracy"); + }); + } + } +} diff --git a/test/TensorFlowNET.Examples/Utility/Compress.cs b/test/TensorFlowNET.Examples/Utility/Compress.cs index bc38434b..95eb0ddf 100644 --- a/test/TensorFlowNET.Examples/Utility/Compress.cs +++ b/test/TensorFlowNET.Examples/Utility/Compress.cs @@ -1,4 +1,20 @@ -using ICSharpCode.SharpZipLib.Core; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Tar; using System; diff --git a/test/TensorFlowNET.Examples/Utility/DataSet.cs b/test/TensorFlowNET.Examples/Utility/DataSet.cs deleted file mode 100644 index 6b6b8769..00000000 --- a/test/TensorFlowNET.Examples/Utility/DataSet.cs +++ /dev/null @@ -1,86 +0,0 @@ -using NumSharp; -using System; -using System.Collections.Generic; -using System.Text; -using Tensorflow; - -namespace TensorFlowNET.Examples.Utility -{ - public class DataSet - { - private int _num_examples; - public int num_examples => _num_examples; - private int _epochs_completed; - public int epochs_completed => _epochs_completed; - private int _index_in_epoch; - public int index_in_epoch => _index_in_epoch; - private NDArray _images; - public NDArray images => _images; - private NDArray _labels; - public NDArray labels => _labels; - - public DataSet(NDArray images, NDArray labels, TF_DataType dtype, bool reshape) - { - _num_examples = images.shape[0]; - images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]); - images.astype(dtype.as_numpy_datatype()); - images = np.multiply(images, 1.0f / 255.0f); - - labels.astype(dtype.as_numpy_datatype()); - - _images = images; - _labels = labels; - _epochs_completed = 0; - _index_in_epoch = 0; - } - - public (NDArray, NDArray) next_batch(int batch_size, bool fake_data = false, bool shuffle = true) - { - var start = _index_in_epoch; - // Shuffle for the first epoch - if(_epochs_completed == 0 && start == 0 && shuffle) - { - var perm0 = np.arange(_num_examples); - np.random.shuffle(perm0); - _images = images[perm0]; - _labels = labels[perm0]; - } - - // Go to the next epoch - if (start + batch_size > _num_examples) - { - // Finished epoch - _epochs_completed += 1; - - // Get the rest examples in this epoch - var rest_num_examples = _num_examples - start; - //var images_rest_part = _images[np.arange(start, _num_examples)]; - //var labels_rest_part = _labels[np.arange(start, _num_examples)]; - // Shuffle the data - if (shuffle) - { - var perm = np.arange(_num_examples); - np.random.shuffle(perm); - _images = images[perm]; - _labels = labels[perm]; - } - - start = 0; - _index_in_epoch = batch_size - rest_num_examples; - var end = _index_in_epoch; - var images_new_part = _images[np.arange(start, end)]; - var labels_new_part = _labels[np.arange(start, end)]; - - /*return (np.concatenate(new float[][] { images_rest_part.Data(), images_new_part.Data() }, axis: 0), - np.concatenate(new float[][] { labels_rest_part.Data(), labels_new_part.Data() }, axis: 0));*/ - return (images_new_part, labels_new_part); - } - else - { - _index_in_epoch += batch_size; - var end = _index_in_epoch; - return (_images[np.arange(start, end)], _labels[np.arange(start, end)]); - } - } - } -} diff --git a/test/TensorFlowNET.Examples/Utility/DataSetMnist.cs b/test/TensorFlowNET.Examples/Utility/DataSetMnist.cs new file mode 100644 index 00000000..0825a702 --- /dev/null +++ b/test/TensorFlowNET.Examples/Utility/DataSetMnist.cs @@ -0,0 +1,98 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.Examples.Utility +{ + public class DataSetMnist : IDataSet + { + public int num_examples { get; } + + public int epochs_completed { get; private set; } + public int index_in_epoch { get; private set; } + public NDArray data { get; private set; } + public NDArray labels { get; private set; } + + public DataSetMnist(NDArray images, NDArray labels, TF_DataType dtype, bool reshape) + { + num_examples = images.shape[0]; + images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]); + images.astype(dtype.as_numpy_datatype()); + images = np.multiply(images, 1.0f / 255.0f); + + labels.astype(dtype.as_numpy_datatype()); + + data = images; + this.labels = labels; + epochs_completed = 0; + index_in_epoch = 0; + } + + public (NDArray, NDArray) next_batch(int batch_size, bool fake_data = false, bool shuffle = true) + { + var start = index_in_epoch; + // Shuffle for the first epoch + if(epochs_completed == 0 && start == 0 && shuffle) + { + var perm0 = np.arange(num_examples); + np.random.shuffle(perm0); + data = data[perm0]; + labels = labels[perm0]; + } + + // Go to the next epoch + if (start + batch_size > num_examples) + { + // Finished epoch + epochs_completed += 1; + + // Get the rest examples in this epoch + var rest_num_examples = num_examples - start; + //var images_rest_part = _images[np.arange(start, _num_examples)]; + //var labels_rest_part = _labels[np.arange(start, _num_examples)]; + // Shuffle the data + if (shuffle) + { + var perm = np.arange(num_examples); + np.random.shuffle(perm); + data = data[perm]; + labels = labels[perm]; + } + + start = 0; + index_in_epoch = batch_size - rest_num_examples; + var end = index_in_epoch; + var images_new_part = data[np.arange(start, end)]; + var labels_new_part = labels[np.arange(start, end)]; + + /*return (np.concatenate(new float[][] { images_rest_part.Data(), images_new_part.Data() }, axis: 0), + np.concatenate(new float[][] { labels_rest_part.Data(), labels_new_part.Data() }, axis: 0));*/ + return (images_new_part, labels_new_part); + } + else + { + index_in_epoch += batch_size; + var end = index_in_epoch; + return (data[np.arange(start, end)], labels[np.arange(start, end)]); + } + } + } +} diff --git a/test/TensorFlowNET.Examples/Utility/Datasets.cs b/test/TensorFlowNET.Examples/Utility/Datasets.cs index 660e40db..93ca1869 100644 --- a/test/TensorFlowNET.Examples/Utility/Datasets.cs +++ b/test/TensorFlowNET.Examples/Utility/Datasets.cs @@ -1,25 +1,49 @@ -using System; +using NumSharp; +using System; using System.Collections.Generic; using System.Text; namespace TensorFlowNET.Examples.Utility { - public class Datasets + public class Datasets where T : IDataSet { - private DataSet _train; - public DataSet train => _train; + private T _train; + public T train => _train; - private DataSet _validation; - public DataSet validation => _validation; + private T _validation; + public T validation => _validation; - private DataSet _test; - public DataSet test => _test; + private T _test; + public T test => _test; - public Datasets(DataSet train, DataSet validation, DataSet test) + public Datasets(T train, T validation, T test) { _train = train; _validation = validation; _test = test; } + + public (NDArray, NDArray) Randomize(NDArray x, NDArray y) + { + var perm = np.random.permutation(y.shape[0]); + + np.random.shuffle(perm); + return (train.data[perm], train.labels[perm]); + } + + /// + /// selects a few number of images determined by the batch_size variable (if you don't know why, read about Stochastic Gradient Method) + /// + /// + /// + /// + /// + /// + public (NDArray, NDArray) GetNextBatch(NDArray x, NDArray y, int start, int end) + { + var x_batch = x[$"{start}:{end}"]; + var y_batch = y[$"{start}:{end}"]; + return (x_batch, y_batch); + } } } diff --git a/test/TensorFlowNET.Examples/Utility/IDataSet.cs b/test/TensorFlowNET.Examples/Utility/IDataSet.cs new file mode 100644 index 00000000..2815bf11 --- /dev/null +++ b/test/TensorFlowNET.Examples/Utility/IDataSet.cs @@ -0,0 +1,13 @@ +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; + +namespace TensorFlowNET.Examples.Utility +{ + public interface IDataSet + { + NDArray data { get; } + NDArray labels { get; } + } +} diff --git a/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs b/test/TensorFlowNET.Examples/Utility/MNIST.cs similarity index 80% rename from test/TensorFlowNET.Examples/Utility/MnistDataSet.cs rename to test/TensorFlowNET.Examples/Utility/MNIST.cs index 41728813..2a61fe77 100644 --- a/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs +++ b/test/TensorFlowNET.Examples/Utility/MNIST.cs @@ -1,4 +1,20 @@ -using NumSharp; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using NumSharp; using System; using System.Collections.Generic; using System.IO; @@ -8,14 +24,14 @@ using Tensorflow; namespace TensorFlowNET.Examples.Utility { - public class MnistDataSet + public class MNIST { private const string DEFAULT_SOURCE_URL = "https://storage.googleapis.com/cvdf-datasets/mnist/"; private const string TRAIN_IMAGES = "train-images-idx3-ubyte.gz"; private const string TRAIN_LABELS = "train-labels-idx1-ubyte.gz"; private const string TEST_IMAGES = "t10k-images-idx3-ubyte.gz"; private const string TEST_LABELS = "t10k-labels-idx1-ubyte.gz"; - public static Datasets read_data_sets(string train_dir, + public static Datasets read_data_sets(string train_dir, bool one_hot = false, TF_DataType dtype = TF_DataType.TF_FLOAT, bool reshape = true, @@ -24,9 +40,9 @@ namespace TensorFlowNET.Examples.Utility int? test_size = null, string source_url = DEFAULT_SOURCE_URL) { - if (train_size!=null && validation_size >= train_size) - throw new ArgumentException("Validation set should be smaller than training set"); - + if (train_size!=null && validation_size >= train_size) + throw new ArgumentException("Validation set should be smaller than training set"); + Web.Download(source_url + TRAIN_IMAGES, train_dir, TRAIN_IMAGES); Compress.ExtractGZip(Path.Join(train_dir, TRAIN_IMAGES), train_dir); var train_images = extract_images(Path.Join(train_dir, TRAIN_IMAGES.Split('.')[0]), limit: train_size); @@ -49,11 +65,11 @@ namespace TensorFlowNET.Examples.Utility train_images = train_images[np.arange(validation_size, end)]; train_labels = train_labels[np.arange(validation_size, end)]; - var train = new DataSet(train_images, train_labels, dtype, reshape); - var validation = new DataSet(validation_images, validation_labels, dtype, reshape); - var test = new DataSet(test_images, test_labels, dtype, reshape); + var train = new DataSetMnist(train_images, train_labels, dtype, reshape); + var validation = new DataSetMnist(validation_images, validation_labels, dtype, reshape); + var test = new DataSetMnist(test_images, test_labels, dtype, reshape); - return new Datasets(train, validation, test); + return new Datasets(train, validation, test); } public static NDArray extract_images(string file, int? limit=null) diff --git a/test/TensorFlowNET.Examples/Utility/PbtxtParser.cs b/test/TensorFlowNET.Examples/Utility/PbtxtParser.cs index b994dd76..cb5857f3 100644 --- a/test/TensorFlowNET.Examples/Utility/PbtxtParser.cs +++ b/test/TensorFlowNET.Examples/Utility/PbtxtParser.cs @@ -1,4 +1,20 @@ -using Newtonsoft.Json; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; diff --git a/test/TensorFlowNET.Examples/Utility/Web.cs b/test/TensorFlowNET.Examples/Utility/Web.cs index e2155e93..95e2c762 100644 --- a/test/TensorFlowNET.Examples/Utility/Web.cs +++ b/test/TensorFlowNET.Examples/Utility/Web.cs @@ -1,4 +1,20 @@ -using System; +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/test/TensorFlowNET.Examples/python/neural_network.py b/test/TensorFlowNET.Examples/python/neural_network.py new file mode 100644 index 00000000..ac9597ca --- /dev/null +++ b/test/TensorFlowNET.Examples/python/neural_network.py @@ -0,0 +1,164 @@ + +# imports +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt + +img_h = img_w = 28 # MNIST images are 28x28 +img_size_flat = img_h * img_w # 28x28=784, the total number of pixels +n_classes = 10 # Number of classes, one class per digit + +def load_data(mode='train'): + """ + Function to (download and) load the MNIST data + :param mode: train or test + :return: images and the corresponding labels + """ + from tensorflow.examples.tutorials.mnist import input_data + mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) + if mode == 'train': + x_train, y_train, x_valid, y_valid = mnist.train.images, mnist.train.labels, \ + mnist.validation.images, mnist.validation.labels + return x_train, y_train, x_valid, y_valid + elif mode == 'test': + x_test, y_test = mnist.test.images, mnist.test.labels + return x_test, y_test + +def randomize(x, y): + """ Randomizes the order of data samples and their corresponding labels""" + permutation = np.random.permutation(y.shape[0]) + shuffled_x = x[permutation, :] + shuffled_y = y[permutation] + return shuffled_x, shuffled_y + +def get_next_batch(x, y, start, end): + x_batch = x[start:end] + y_batch = y[start:end] + return x_batch, y_batch + +# Load MNIST data +x_train, y_train, x_valid, y_valid = load_data(mode='train') +print("Size of:") +print("- Training-set:\t\t{}".format(len(y_train))) +print("- Validation-set:\t{}".format(len(y_valid))) + +print('x_train:\t{}'.format(x_train.shape)) +print('y_train:\t{}'.format(y_train.shape)) +print('x_train:\t{}'.format(x_valid.shape)) +print('y_valid:\t{}'.format(y_valid.shape)) + +print(y_valid[:5, :]) + +# Hyper-parameters +epochs = 10 # Total number of training epochs +batch_size = 100 # Training batch size +display_freq = 100 # Frequency of displaying the training results +learning_rate = 0.001 # The optimization initial learning rate + +h1 = 200 # number of nodes in the 1st hidden layer + +# weight and bais wrappers +def weight_variable(name, shape): + """ + Create a weight variable with appropriate initialization + :param name: weight name + :param shape: weight shape + :return: initialized weight variable + """ + initer = tf.truncated_normal_initializer(stddev=0.01) + return tf.get_variable('W_' + name, + dtype=tf.float32, + shape=shape, + initializer=initer) + + +def bias_variable(name, shape): + """ + Create a bias variable with appropriate initialization + :param name: bias variable name + :param shape: bias variable shape + :return: initialized bias variable + """ + initial = tf.constant(0., shape=shape, dtype=tf.float32) + return tf.get_variable('b_' + name, + dtype=tf.float32, + initializer=initial) + +def fc_layer(x, num_units, name, use_relu=True): + """ + Create a fully-connected layer + :param x: input from previous layer + :param num_units: number of hidden units in the fully-connected layer + :param name: layer name + :param use_relu: boolean to add ReLU non-linearity (or not) + :return: The output array + """ + in_dim = x.get_shape()[1] + W = weight_variable(name, shape=[in_dim, num_units]) + b = bias_variable(name, [num_units]) + layer = tf.matmul(x, W) + layer += b + if use_relu: + layer = tf.nn.relu(layer) + return layer + +# Create the graph for the linear model +# Placeholders for inputs (x) and outputs(y) +x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='X') +y = tf.placeholder(tf.float32, shape=[None, n_classes], name='Y') + +# Create a fully-connected layer with h1 nodes as hidden layer +fc1 = fc_layer(x, h1, 'FC1', use_relu=True) +# Create a fully-connected layer with n_classes nodes as output layer +output_logits = fc_layer(fc1, n_classes, 'OUT', use_relu=False) + +# Define the loss function, optimizer, and accuracy +logits = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output_logits) +loss = tf.reduce_mean(logits, name='loss') +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, name='Adam-op').minimize(loss) +correct_prediction = tf.equal(tf.argmax(output_logits, 1), tf.argmax(y, 1), name='correct_pred') +accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy') + +# Network predictions +cls_prediction = tf.argmax(output_logits, axis=1, name='predictions') + +# export graph +#tf.train.export_meta_graph(filename='neural_network.meta', graph=tf.get_default_graph(), clear_extraneous_savers= True, as_text = True) + +# Create the op for initializing all variables +init = tf.global_variables_initializer() + +# Create an interactive session (to keep the session in the other cells) +sess = tf.InteractiveSession() +# Initialize all variables +sess.run(init) +# Number of training iterations in each epoch +num_tr_iter = int(len(y_train) / batch_size) +for epoch in range(epochs): + print('Training epoch: {}'.format(epoch + 1)) + # Randomly shuffle the training data at the beginning of each epoch + x_train, y_train = randomize(x_train, y_train) + for iteration in range(num_tr_iter): + start = iteration * batch_size + end = (iteration + 1) * batch_size + x_batch, y_batch = get_next_batch(x_train, y_train, start, end) + + # Run optimization op (backprop) + feed_dict_batch = {x: x_batch, y: y_batch} + sess.run(optimizer, feed_dict=feed_dict_batch) + + if iteration % display_freq == 0: + # Calculate and display the batch loss and accuracy + loss_batch, acc_batch = sess.run([loss, accuracy], + feed_dict=feed_dict_batch) + + print("iter {0:3d}:\t Loss={1:.2f},\tTraining Accuracy={2:.01%}". + format(iteration, loss_batch, acc_batch)) + + # Run validation after every epoch + feed_dict_valid = {x: x_valid[:1000], y: y_valid[:1000]} + loss_valid, acc_valid = sess.run([loss, accuracy], feed_dict=feed_dict_valid) + print('---------------------------------------------------------') + print("Epoch: {0}, validation loss: {1:.2f}, validation accuracy: {2:.01%}". + format(epoch + 1, loss_valid, acc_valid)) + print('---------------------------------------------------------') \ No newline at end of file diff --git a/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs b/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs index a267324e..4c71d7e2 100644 --- a/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs +++ b/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs @@ -83,22 +83,13 @@ namespace TensorFlowNET.ExamplesTests new NearestNeighbor() { Enabled = true, TrainSize = 500, ValidationSize = 100, TestSize = 100 }.Run(); } - [Ignore] [TestMethod] - public void TextClassificationTrain() - { - tf.Graph().as_default(); - new TextClassificationTrain() { Enabled = true, DataLimit=100 }.Run(); - } - + public void WordCnnTextClassification() + => new CnnTextClassification { Enabled = true, ModelName = "word_cnn", DataLimit =100 }.Run(); [TestMethod] - public void CnnTextClassificationTrain() - { - tf.Graph().as_default(); - new CnnTextClassification() { Enabled = true, IsImportingGraph = false }.Run(); - } - + public void CharCnnTextClassification() + => new CnnTextClassification { Enabled = true, ModelName = "char_cnn", DataLimit = 100 }.Run(); [Ignore] [TestMethod] diff --git a/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj b/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj index f76ca132..b8917b91 100644 --- a/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj @@ -16,7 +16,7 @@ - +