theflofly / dnn_tensorflow_cpp Goto Github PK
View Code? Open in Web Editor NEWThis project is a simple deep neural network trained using only TensorFlow C++.
This project is a simple deep neural network trained using only TensorFlow C++.
Hey there,
I am implementing a multilayer perceptron with the AddSymbolicGradients, for the or and x-or it works with multiclass but for mnist data it doesnt train the loss for the same image each epoch has the loss for the output with shape 10
Loss: 0.875 0.84375 0.9375 0.90625 0.96875 0.9375 0.875 0.875 0.875 0.90625
Loss: 0.96875 0.8125 0.90625 0.875 0.90625 0.875 0.90625 0.96875 0.9375 0.84375
Loss: 0.9375 0.8125 0.90625 0.875 0.9375 0.96875 0.875 0.8125 0.96875 0.90625
Loss: 0.875 0.84375 0.9375 0.90625 0.96875 0.9375 0.875 0.875 0.875 0.90625
Loss: 0.96875 0.8125 0.90625 0.875 0.90625 0.875 0.90625 0.96875 0.9375 0.84375
Loss: 0.9375 0.8125 0.90625 0.875 0.9375 0.96875 0.875 0.8125 0.96875 0.90625
...
My code looks
void Model::train(Tensor imageTensor, Tensor labelTensor, int maxEpochs, float learningRate, int batchSize) {
if (imageTensor.dim_size(0) != labelTensor.dim_size(0)) {
std::cerr << "Image und label dataset size must fit together";
std::exit(EXIT_FAILURE);
}
Tensor imageBatches, labelBatches;
std::tie(imageBatches, labelBatches) = getBatches(batchSize, imageTensor, labelTensor);
Scope lossScope = scope.NewSubScope("Training");
auto loss = Mean(lossScope.WithOpName("Loss"), SquaredDifference(lossScope.WithOpName("Sigmoid-Cross-Entropy"), model, *this->labels), {0});
std::cout << "Image batches size: " << imageBatches.shape() << std::endl;
std::vector<Output> apply_gradients = this->backpropagation(lossScope,learningRate, loss);
std::cout << "Training started" << std::endl;
int dataSize = imageBatches.dim_size(0);
std::vector<Tensor> outputs;
for (int i = 1; i <= maxEpochs; i++) {
auto lossValue = 0;
for (int64_t num = 0; num < dataSize; num++) {
vector<Tensor> output1;
//auto d1 = DeepCopy(scope, imageBatches.SubSlice(num));
//auto d2 = DeepCopy(scope, labelBatches.SubSlice(num));
//TF_CHECK_OK(session->Run({d1, d2}, &output1));
Tensor imageBatch(imageBatches.SubSlice(num));
Tensor labelBatch(labelBatches.SubSlice(num));
TF_CHECK_OK(session->Run({{*features, imageBatch}, {*this->labels, labelBatch}}, apply_gradients, {}, nullptr));
if (num % 1000 == 0) {
//TF_CHECK_OK(session->Run({{*features, inputFeatures[num]}, {*this->labels, labels[num]}}, {loss}, &outputs));
TF_CHECK_OK(session->Run({{*features, imageBatch}, {*this->labels, labelBatch}}, {loss}, &outputs));
std::cout << "Loss: " << outputs[0].flat<float>() << std::endl;
}
}
if (i % 100 == 0) {
std::cout << "Epoch " << i << " Loss: " << lossValue << std::endl;
std::cout << " " << std::endl;
}
}
printWeightForNumber(0);
}
std::vector<Output> Model::backpropagation(Scope lossScope, float learningRate, Output loss) {
std::vector<std::shared_ptr<Variable>> weights = getAllLayerWeights();
std::vector<Output> gradients;
TF_CHECK_OK(AddSymbolicGradients(scope.WithOpName("Gradients"), {loss}, {*weights[0]}, &gradients));
std::vector<Output> apply_gradients;
for (int i = 0; i < weights.size(); i++) {
Output apply_gradient = ApplyGradientDescent(lossScope.WithOpName("Apply-Gradients-" + std::to_string(i)), *weights[i], Cast(scope, learningRate, DT_FLOAT), gradients[i]);
apply_gradients.push_back(apply_gradient);
}
return apply_gradients;
}
I would appreciate it if you could help me as soon as possible.
Very instructive work
i just build the program well
when the program run to the line 90:
TF_CHECK_OK(session.Run({{x, x_data}, {y, y_data}}, {apply_w1, apply_w2, apply_w3, apply_b1, apply_b2, apply_b3}, nullptr));
the system always told me two error:
some time is Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.92GiB. Current allocation summary follows.
another time is Non-OK-status: session.Run({ { x, x_data },{ y, y_data } }, { apply_w1, apply_w2, apply_w3, apply_b1, apply_b2, apply_b3, layer_3 }, nullptr) status: Invalid argument: Incompatible shapes: [2] vs. [0]
I'm not sure which error is right.
So what should i do ?
@theflofly Hello, I compiled tensorflow 2.0, and then run this code. The program reported an error. I didn't find the problem. Can you help solve this problem? My Operating environment is Linux.
ERROR:
2020-10-24 14:42:57.130012: F tensorflow/core/framework/tensor.cc:693] Check failed: dtype() == expected_dtype (3 vs. 1) float expected, got int32
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.