1- using System ;
1+ using Google . Protobuf ;
2+ using Google . Protobuf . Collections ;
3+ using System ;
24using System . Collections . Generic ;
35using System . Linq ;
46using System . Text ;
@@ -99,7 +101,7 @@ private void _init_from_args(object initial_value,
99101 if ( initial_value is null )
100102 throw new ValueError ( "initial_value must be specified." ) ;
101103
102- var init_from_fn = false ;
104+ var init_from_fn = initial_value . GetType ( ) . Name == "Func`1" ;
103105
104106 if ( collections == null )
105107 {
@@ -115,12 +117,27 @@ private void _init_from_args(object initial_value,
115117 collections . Add ( ops . GraphKeys . TRAINABLE_VARIABLES ) ;
116118
117119 ops . init_scope ( ) ;
118- var values = init_from_fn ? new List < object > ( ) : new List < object > { initial_value } ;
119- Python . with < ops . name_scope > ( new ops . name_scope ( name , "Variable" , values ) , scope =>
120+ var values = init_from_fn ? new object [ 0 ] : new object [ ] { initial_value } ;
121+ with < ops . name_scope > ( new ops . name_scope ( name , "Variable" , values ) , scope =>
120122 {
123+ name = scope ;
121124 if ( init_from_fn )
122125 {
123-
126+ // Use attr_scope and device(None) to simulate the behavior of
127+ // colocate_with when the variable we want to colocate with doesn't
128+ // yet exist.
129+ string true_name = ops . _name_from_scope_name ( name ) ;
130+ var attr = new AttrValue
131+ {
132+ List = new AttrValue . Types . ListValue ( )
133+ } ;
134+ attr . List . S . Add ( ByteString . CopyFromUtf8 ( $ "loc:{ true_name } ") ) ;
135+ with < ops . name_scope > ( new ops . name_scope ( "Initializer" ) , scope2 =>
136+ {
137+ _initial_value = ( initial_value as Func < Tensor > ) ( ) ;
138+ _initial_value = ops . convert_to_tensor ( _initial_value , name : "initial_value" , dtype : dtype ) ;
139+ _variable = state_ops . variable_op_v2 ( _initial_value . shape , _initial_value . dtype . as_base_dtype ( ) , name : name ) ;
140+ } ) ;
124141 }
125142 // Or get the initial value from a Tensor or Python object.
126143 else
@@ -135,7 +152,9 @@ private void _init_from_args(object initial_value,
135152 // Manually overrides the variable's shape with the initial value's.
136153 if ( validate_shape )
137154 {
138- var initial_value_shape = _initial_value . shape ;
155+ var initial_value_shape = _initial_value . getShape ( ) ;
156+ if ( ! initial_value_shape . is_fully_defined ( ) )
157+ throw new ValueError ( $ "initial_value must have a shape specified: { _initial_value } ") ;
139158 }
140159
141160 // If 'initial_value' makes use of other variables, make sure we don't
0 commit comments