
    iN+              	          d dl Z d dlmZ d dlmZ d dlZd dlmZ d dlZ	d dl
mZmZmZ d dlmZ d Z G d dej"                        Z G d dej"                        ZddZd Zedk(  r e       Z ej0                   ej2                          ej4                  dd      g      Zej8                  j;                  ddde      Z eedd      Z e       Z  ed
e jB                        Z" e#d
      D ]  Z$ ee e"ed	dd
        ee e        yy)    N)
DataLoaderSubsetTensorDataset)deepcopyc                     t        j                  t        j                         t        j                  dd      g      } t        j
                  j                  ddd|       }t        d      D ci c]  }|g  }}t        |      D ].  \  }\  }}t        ||         dk  s||   j                  |       0 |j                         D cg c]  }|D ]  }|  }}}t        ||      S c c}w c c}}w )NgHPs?gec]?g~jt?gۊe?ggDio?g|?5^?../dataTroottraindownload	transform
   )
transformsComposeToTensor	NormalizetorchvisiondatasetsCIFAR10range	enumeratelenappendvaluesr   )	r   
full_traincindices_per_classidx_labelindicesselected_indicess	            ex01.pyget_10_shot_cifar10r&      s    ""57OP$ I %%--9DSWcl-mJ(-b	21B22$Z0 1Za '(2-e$++C01 ,=+C+C+EYQXY#YYY*.// 3 Zs   .
C(	C-c                   0     e Zd Z fdZd Zd Zd Z xZS )	HybridNetc                 &   t         |           t        j                  t        j                  dddd      t        j
                         t        j                  d      t        j                  dddd      t        j
                         t        j                  d      t        j                  dddd      t        j
                         t        j                  d      	      | _        d| _	        t        j                  | j                  d      | _        y )	N          )padding   @      r   )super__init__nn
SequentialConv2dReLU	MaxPool2dAdaptiveAvgPool2dfeaturesfc_inLinear
classifier)self	__class__s    r%   r2   zHybridNet.__init__   s    IIaQ*BGGIr||AIIb"a+RWWYQIIb#q!,bggi9M9Ma9P

 
))DJJ3    c                     | j                  |      j                  |j                  d      d      }| j                  |      S )Nr   )r9   viewsizer<   )r=   xfeats      r%   forwardzHybridNet.forward'   s4    }}Q$$QVVAY3t$$r?   c                 H    t        | j                  j                               S N)listr9   
parametersr=   s    r%   get_adaptive_paramszHybridNet.get_adaptive_params+   s    DMM,,.//r?   c                 H    t        | j                  j                               S rH   )rI   r<   rJ   rK   s    r%   get_stable_paramszHybridNet.get_stable_params.   s    DOO..011r?   )__name__
__module____qualname__r2   rF   rL   rN   __classcell__r>   s   @r%   r(   r(      s    
4%02r?   r(   c                   *     e Zd ZdZd fd	Zd Z xZS )DummySamplesz:Learnable dummy samples (one per class) as latent vectors.c                     t         |           t        j                  t	        j
                  ||      dz        | _        || _        y )Ng?)r1   r2   r3   	Parametertorchrandndummies
latent_dim)r=   num_classesr[   r>   s      r%   r2   zDummySamples.__init__4   s5    ||EKKZ$H3$NO$r?   c                      | j                   |   S rH   )rZ   )r=   	class_idxs     r%   rF   zDummySamples.forward9   s    ||I&&r?   )r   r0   )rO   rP   rQ   __doc__r2   rF   rR   rS   s   @r%   rU   rU   2   s    D%
'r?   rU      r   c                 	   t        t        |            D cg c]
  }||   d    }}t        d      D ci c]  }|g  }	}t        |      D ]  \  }
}|	|   j                  |
        t	        d |	j                         D              }g }t        |      D ])  }t        d      D ]  }|j                  |	|   |           + t        dt        |      |      D cg c]
  }||||z     }}|D cg c]  }t        j                  |       }}t        j                  | j                         d      }t        j                  | j                         d      }t        j                  |j                         d      }| j                          t        |      D ]@  }t        j                  t        |            }d }d }|D ]   }||j                            }t        j                   |D cg c]  }|t#        |         d    c}      }t        j                  |D cg c]  }|t#        |         d    c}      }t        j                  t        |            }t        |      } t        j$                  t        j&                  |       | d	      }!||!   }"||!   }#| j)                  |"      j+                  |"j-                  d      d
      }$ |t        j.                  d            }%t        j0                  |$|%gd      }&t        j.                  d      }'t        j0                  |#|'g      }(| j)                  |      j+                  |j-                  d      d
      })| j3                  |)      }* t5        j6                         |*|      }+t        |      D ]l  },| j3                  |&j9                               }- t5        j6                         |-|(      }.|j;                          |.j=                          |j?                          n |j;                          |+j=                          |j?                          || j)                  |      j+                  |j-                  d      d
      }/| j3                  |/      }0 t5        j6                         |0|      }1|j;                          |1j=                          |j?                          |}|} tA        d|dz    d| d       C y c c}w c c}w c c}w c c}w c c}w c c}w )Nr,   r   c              3   2   K   | ]  }t        |        y wrH   )r   ).0r#   s     r%   	<genexpr>ztrain_hybrid.<locals>.<genexpr>D   s     P7#g,Ps   r   gMbP?)lrg{Gz?T)replacementrA   )dimzEpoch /z
 completed)!r   r   r   r   minr   rX   tensoroptimAdamrL   rN   rJ   r   randpermitemstackintmultinomialonesr9   rB   rC   arangecatr<   r3   CrossEntropyLossdetach	zero_gradbackwardstepprint)2modeldummy_samplesreal_dataset
num_epochs
batch_sizeinner_stepsireal_labelsr   indices_by_classr    r"   shots_per_classordered_indicesshot_idxr^   startbatch_indicesbatch
batch_setsopt_adaptive
opt_stable	opt_dummyepochpermprev_batch_realprev_batch_labels	batch_idxreal_indicesreal_imagesreal_labels_timbalanced_indicesnum_realimba_choiceimba_real_imagesimba_real_labels	feat_realdummy_latentscombined_featdummy_targetscombined_targetsfeat_balancedlogits_balancedloss_stabler!   logits_combinedloss_adaptive	feat_prevlogits_prev	meta_losss2                                                     r%   train_hybridr   =   s    05S5F/GH!<?1%HKH',Ry1!211, ,
U&&s+,P6F6M6M6OPPOO/* Jr 	JI""#3I#>x#HI	JJ 1c/2J? 	ej01M  4AA%%,,u%AJA ::e779dCLE335$?J

=335$?I
 
KKMz" [9~~c*o.  S	.I%inn&67L++&U1|CF';A'>&UVK!LL<)Xa,s1v*>q*A)XYM "'K0@!A ;'H++EJJx,@(X\]K*;7,[9 '78==>N>S>STU>VXZ[I *%,,r*:;M "IIy-&@aHM!LL,M$yy*:M)JK "NN;7<<[=M=Ma=PRTUM#..}=O/"--/OK ;' $"'"2"2=3G3G3I"J 5 3 3 5oGW X&&(&&(!!#$   "  "OO * "NN?;@@AUAUVWAXZ\]	#..y91B//1+?PQ	##%""$  *O -gS	.j 	uQwiqJ78w[95 I1 B( 'V)Xs"   S
S S%.S*(S/S4c                 x   | j                          d}d}t        j                         5  |D ]^  \  }} | |      }t        j                  |d      \  }}||j	                  d      z  }|||k(  j                         j                         z  }` 	 d d d        d|z  |z  }	t        d|	dd       |	S # 1 sw Y   #xY w)Nr   r,   d   zTest Accuracy: z.2f%)evalrX   no_gradmaxrC   sumrn   rz   )
r{   test_loadercorrecttotalimageslabelsoutputsr!   	predictedaccs
             r%   testr      s    	JJLGE	 :) 	:NFFFmG 99Wa0LAyV[[^#E	V+0027799G		:: -%
C	OC9A
&'J: :s   A$B00B9__main__r   r	   r
   FTr   r   )r   shuffle)r\   r[   )r~   r   r   )r`   r   r   )%rX   torch.nnr3   torch.optimrk   r   torchvision.transformsr   numpynptorch.utils.datar   r   r   copyr   r&   Moduler(   rU   r   r   rO   	train_setr   r   r   test_transformr   r   test_setr   r{   r:   r|   r   r!    r?   r%   <module>r      s       +  > > 02		 20'299 'x9v z#%I'Z''

57OP) N ##++%RVbp+qHX#uEKKE REKKHM 2Y !UM9PSacdUK ! r?   