/
githubmirror
/
cmssw
Обзор
Документация
Войти
/
githubmirror
/
cmssw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
PhysicsTools/TensorFlow/test/testConstSession.cc
68 строк
2 KB
Davide Valsecchi
Improved options struct for TF session
05 апр 2023, 00:24
05 апр 2023, 00:24
8a546bc
Код
Авторство
О чём код?
/* * Tests for working with constant sessions. * For more info, see https://gitlab.cern.ch/mrieger/CMSSW-DNN. * * Author: Marcel Rieger */ #include <stdexcept> #include <cppunit/extensions/HelperMacros.h> #include "PhysicsTools/TensorFlow/interface/TensorFlow.h" #include "testBase.h" class testConstSession : public testBase { CPPUNIT_TEST_SUITE(testConstSession); CPPUNIT_TEST(test); CPPUNIT_TEST_SUITE_END(); public: std::string pyScript() const override; void test() override; }; CPPUNIT_TEST_SUITE_REGISTRATION(testConstSession); std::string testConstSession::pyScript() const { return "createconstantgraph.py"; } void testConstSession::test() { std::string pbFile = dataPath_ + "/constantgraph.pb"; std::cout << "Testing CPU backend" << std::endl; tensorflow::Backend backend = tensorflow::Backend::cpu; // load the graph tensorflow::Options options{backend}; tensorflow::GraphDef* graphDef = tensorflow::loadGraphDef(pbFile); CPPUNIT_ASSERT(graphDef != nullptr); // create a new session and add the graphDef const tensorflow::Session* session = tensorflow::createSession(graphDef, options); CPPUNIT_ASSERT(session != nullptr); // example evaluation tensorflow::Tensor input(tensorflow::DT_FLOAT, {1, 10}); float* d = input.flat<float>().data(); for (size_t i = 0; i < 10; i++, d++) { *d = float(i); } tensorflow::Tensor scale(tensorflow::DT_FLOAT, {}); scale.scalar<float>()() = 1.0; std::vector<tensorflow::Tensor> outputs; // run using the convenience helper outputs.clear(); tensorflow::run(session, {{"input", input}, {"scale", scale}}, {"output"}, &outputs); CPPUNIT_ASSERT(outputs.size() == 1); std::cout << outputs[0].DebugString() << std::endl; CPPUNIT_ASSERT(outputs[0].matrix<float>()(0, 0) == 46.); // check for exception CPPUNIT_ASSERT_THROW(tensorflow::run(session, {{"foo", input}}, {"output"}, &outputs), cms::Exception); // cleanup CPPUNIT_ASSERT(tensorflow::closeSession(session)); CPPUNIT_ASSERT(session == nullptr); delete graphDef; }