AI-TensorFlow-Libtensorflow

 view release on metacpan or  search on metacpan

t/upstream/CAPI/018_ImportGraphDef.t  view on Meta::CPAN

#!/usr/bin/env perl

use Test2::V0;
use lib 't/lib';
use TF_TestQuiet;
use TF_Utils;
use aliased 'AI::TensorFlow::Libtensorflow';
use AI::TensorFlow::Libtensorflow::DataType qw(INT32);

use AI::TensorFlow::Libtensorflow::Lib::Types qw(
	TFOutput TFOutputFromTuple
	TFInput  TFInputFromTuple
);
use Types::Standard qw(HashRef);

my $TFOutput = TFOutput->plus_constructors(
		HashRef, 'New'
	)->plus_coercions(TFOutputFromTuple);
my $TFInput = TFInput->plus_constructors(
		HashRef, 'New'
	)->plus_coercions(TFInputFromTuple);

subtest "(CAPI, ImportGraphDef)" => sub {
	my $s = AI::TensorFlow::Libtensorflow::Status->New;
	my $graph = AI::TensorFlow::Libtensorflow::Graph->New;

	note 'Create a simple graph.';
	TF_Utils::Placeholder($graph, $s);
	TF_Utils::AssertStatusOK($s);
	ok $graph->OperationByName( 'feed' ), 'got feed operation from graph';
	my $oper = TF_Utils::ScalarConst( $graph, $s, 'scalar', INT32, 3);
	TF_Utils::AssertStatusOK($s);
	ok $graph->OperationByName( 'scalar' ), 'got scalar operation from graph';
	TF_Utils::Neg( $oper, $graph, $s );
	TF_Utils::AssertStatusOK($s);
	ok $graph->OperationByName( 'neg' ), 'got neg operation from graph';

	note 'Export to a GraphDef.';
	my $graph_def = AI::TensorFlow::Libtensorflow::Buffer->New;
	$graph->ToGraphDef( $graph_def, $s );
	TF_Utils::AssertStatusOK($s);

	note 'Import it, with a prefix, in a fresh graph.';
	undef $graph;
	$graph = AI::TensorFlow::Libtensorflow::Graph->New;
	my $opts = AI::TensorFlow::Libtensorflow::ImportGraphDefOptions->New;
	$opts->SetPrefix('imported');
	$graph->ImportGraphDef($graph_def, $opts, $s);
	TF_Utils::AssertStatusOK($s);

	ok my $scalar = $graph->OperationByName('imported/scalar'), 'imported/scalar';
	ok my $feed = $graph->OperationByName('imported/feed'), 'imported/feed';
	ok my $neg = $graph->OperationByName('imported/neg'), 'imported/neg';

	note 'Test basic structure of the imported graph.';
	is $scalar->NumInputs, 0, 'scalar.inputs == 0';
	is $feed->NumInputs, 0, 'feed.inputs == 0';
	is $neg->NumInputs, 1, 'neg.inputs == 1';
	my $neg_input = $neg->Input( $TFInput->coerce([$neg => 0]) );
	is $neg_input, object {
		call sub { shift->oper->Name }, $scalar->Name;
		call index => 0;
	}, 'scalar:0 -> neg:0';



( run in 2.654 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )