Tensorflow 算法优化器验证

原创文章,转载请注明: 转载自慢慢的回味

本文链接地址: Tensorflow 算法优化器验证

通过一个很简单的程序来验算法优化器的功能。如下程序,通过原图:输入数据->取负数->取负数->取倒数->取倒数->得到结果,经过优化计算图直接变成:输入数据->得到结果,计算节点数目大大减小了。

测试程序

程序用于验证算法优化器的功能:
输入数据->取负数->取负数->取倒数->取倒数->得到结果,经过优化计算图直接变成:输入数据->得到结果。
回目录

//============================================================================
// Name        : TensorflowTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
 
#include <iostream>
#include <tensorflow/c/c_api.h>
#include <tensorflow/c/c_test_util.h>
 
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <memory>
#include <vector>
#include <string.h>
 
using namespace std;
 
static void FloatDeallocator(void* data, size_t, void* arg) {
	delete[] static_cast<float*>(data);
}
 
int main() {
	cout << "!!!Hello World!!! ArithOptimizeTest.cpp " << endl; // prints !!!Hello World!!!
	cout << "Hello from TensorFlow C library version" << TF_Version() << endl;
 
	TF_Status* s = TF_NewStatus();
	TF_Graph* graph = TF_NewGraph();
 
	float* constVals = new float[2] { 1, 2 };
	int64_t filterDims[] = { 1, 2 };
	TF_Tensor* constTensor = TF_NewTensor(TF_FLOAT, filterDims, 2, constVals,
			sizeof(float) * 2, &FloatDeallocator, nullptr);
	TF_Operation* input = Placeholder(graph, s, "input", TF_FLOAT);
 
	TF_OperationDescription* neg1 = TF_NewOperation(graph, "Neg", "neg1");
	TF_AddInput(neg1, { input, 0 });
	TF_Operation* neg1op = TF_FinishOperation(neg1, s);
 
	TF_OperationDescription* neg2 = TF_NewOperation(graph, "Neg", "neg2");
	TF_AddInput(neg2, { neg1op, 0 });
	TF_Operation* neg2op = TF_FinishOperation(neg2, s);
 
	TF_OperationDescription* reciprocal1 = TF_NewOperation(graph, "Reciprocal",
			"reciprocal1");
	TF_AddInput(reciprocal1, { neg2op, 0 });
	TF_Operation* reciprocal1op = TF_FinishOperation(reciprocal1, s);
 
	TF_OperationDescription* reciprocal2 = TF_NewOperation(graph, "Reciprocal",
			"reciprocal2");
	TF_AddInput(reciprocal2, { reciprocal1op, 0 });
	TF_Operation* reciprocal2op = TF_FinishOperation(reciprocal2, s);
 
	TF_OperationDescription* ident = TF_NewOperation(graph, "Identity",
			"ident");
	TF_AddInput(ident, { reciprocal2op, 0 });
	TF_Operation* identop = TF_FinishOperation(ident, s);
 
	TF_SessionOptions* opts = TF_NewSessionOptions();
	TF_Session* sess = TF_NewSession(graph, opts, s);
	TF_DeleteSessionOptions(opts);
 
	TF_Output feeds[] = { TF_Output{input,0} };
	TF_Output fetches[] = { TF_Output { identop, 0 } };
 
	const char* handle = nullptr;
	TF_SessionPRunSetup(sess, feeds, TF_ARRAYSIZE(feeds), fetches,
			TF_ARRAYSIZE(fetches), NULL, 0, &handle, s);
	cout << TF_Message(s);
 
	// Feed A and fetch A + 2.
	TF_Output feeds1[] = { TF_Output{input,0} };
	TF_Output fetches1[] = { TF_Output { identop, 0 } };
	TF_Tensor* feedValues1[] = { constTensor };
	TF_Tensor* fetchValues1[1];
	TF_SessionPRun(sess, handle, feeds1, feedValues1, 1, fetches1, fetchValues1,
			1, NULL, 0, s);
	cout << TF_Message(s);
	cout << "Result:" << endl;
	float* data = static_cast<float*>(TF_TensorData(fetchValues1[0]));
	size_t size = TF_TensorByteSize(fetchValues1[0]) / sizeof(float);
	for (size_t i = 0; i < size; i++) {
		cout << data[i] << endl;
	}
 
	// Clean up.
	TF_DeletePRunHandle(handle);
	TF_DeleteSession(sess, s);
	cout << TF_Message(s);
	TF_DeleteGraph(graph);
	TF_DeleteStatus(s);
	return 0;
}

本作品采用知识共享署名 4.0 国际许可协议进行许可。

发表回复