Exemplo n.º 1
0
def baseline_error(): 
	# make the baseline predictor
	#meanactdum = np.sum(data.yeval, axis=0)/data.numtrials
	#meanactdum  = np.mean(data.yeval, axis=0)
	meanactdum  = np.mean(data.ytrain, axis=0)
	
	meanact =  np.reshape(meanactdum, [1,meanactdum.size])
	meanpredict = np.repeat(meanact,data.numeval, axis=0)
	
	#make placeholdfers for baseline 
	y_ = tf.placeholder(tf.float32, shape=(None,data.numcell))
	meanpredict_placeholder = tf.placeholder(tf.float32, shape=(None,data.numcell))
	loss_baseline = buildnet.loss(meanpredict_placeholder,y_)
	loss_baseline_percell = buildnet.losspercell(meanpredict_placeholder,y_)
	
	init = tf.global_variables_initializer()
	sess = tf.Session()
	sess.run(init)
	
	# Evaluate the baseline.
	feed_dict={meanpredict_placeholder: meanpredict, y_: data.yeval}
	loss_baseline_eval = sess.run(loss_baseline, feed_dict=feed_dict)
	loss_baseline_eval_percell = sess.run(loss_baseline_percell, feed_dict=feed_dict)
	#print and return baseline loss
	print('')
	print('Eval data baseline loss = %.4f' % (loss_baseline_eval))
	print("varience of eval")
	vare = np.var(data.yeval, axis = 0)
	print(np.shape(vare) )
	print(np.mean(vare))
	manvare = np.mean(np.mean(np.square(meanpredict-data.yeval)))
	print("man calc varience/loss of eval")
	print(manvare)
	return loss_baseline_eval, loss_baseline_eval_percell
Exemplo n.º 2
0
def run_training(lossbaseline, lossbaselinenueron, model=None, dataset=None):
	if dataset is not None:
		data = dataset

	#start the training
	with tf.Graph().as_default():
		# generate placeholders
		images_placeholder = tf.placeholder(tf.float32, shape=(None, data.numpixx, data.numpixy, 1))
		activity_placeholder = tf.placeholder(tf.float32, shape=(None,data.numcell))
		keep_prob_placeholder = tf.placeholder(tf.float32)
		baselineloss_placeholder = tf.placeholder(tf.float32, shape=(data.numcell))

		# network hyper-parameters as arrays
		#2 conv layers
		if FLAGS.numconvlayer == 1:
			num_filter_list = [FLAGS.conv1] 
			filter_size_list = [FLAGS.conv1size] 
			pool_stride_list = [FLAGS.nstride1] 
			pool_k_list =[FLAGS.nk1]		
		elif FLAGS.numconvlayer == 2:
			num_filter_list = [FLAGS.conv1, FLAGS.conv2] # [16,32]
			filter_size_list = [FLAGS.conv1size, FLAGS.conv2size] # [7,7]
			pool_stride_list = [FLAGS.nstride1, FLAGS.nstride2] # [2,2]
			pool_k_list =[FLAGS.nk1, FLAGS.nk2 ]  # [3, 3]
		elif FLAGS.numconvlayer == 3:
			num_filter_list = [FLAGS.conv1, FLAGS.conv2, FLAGS.conv2] # [16,32]
			filter_size_list = [FLAGS.conv1size, FLAGS.conv2size, FLAGS.conv2size] # [7,7]
			pool_stride_list = [FLAGS.nstride1, FLAGS.nstride2, FLAGS.nstride2] # [2,2]
			pool_k_list =[FLAGS.nk1, FLAGS.nk2, FLAGS.nk2]  # [3, 3]

		#1 all-to-all hidden layer
		dense_list = [FLAGS.hidden1] # [300]
		keep_prob = FLAGS.dropout # 0.55
		numcell = data.numcell

		# if no model passed, use default
		if model is None:

			model = ConvNetDrop(images_placeholder, 
				num_filter_list, filter_size_list, pool_stride_list, 
				pool_k_list, dense_list, keep_prob_placeholder,numcell)
		
		else:
			model(images_placeholder)
		
		print("model shape is")
		print(model.output.get_shape())

		## Add to the Graph the Ops for loss calculation.
		loss = buildnet.loss(model.output, activity_placeholder)
		loss_per_nueron = buildnet.losspercell(model.output, activity_placeholder)
		train_op = buildnet.training(loss, FLAGS.learning_rate)

		# Add the variable initializer Op.
		init = tf.global_variables_initializer()

		# Create a session for running Ops on the Graph.
		sess = tf.Session()
		# Run the Op to initializactivitytraine the variables.
		sess.run(init)

		# setting up recording training progress
		steplist = []
		evallist = []
		trainlist = []
		earlystoplist = []
		rmeanlist = []
		rcelllist = []
    
		## Start the training loop.
		lossearlystopmin = 10e6 # large dummy value use for finding the minimum loss in the early stop data
		for step in xrange(FLAGS.max_steps):
			start_time = time.time()
			batchindex = np.random.permutation(numtrain)[0:numbatch]
			xtrainbatch = data.xtrain[batchindex ,:,:,:]
			ytrainbatch = data.ytrain[batchindex ,:]
      
			feed_dict={
				images_placeholder: xtrainbatch,
				activity_placeholder: ytrainbatch,
				keep_prob_placeholder: keep_prob
				}

			_, loss_value = sess.run([train_op, loss],
                               feed_dict=feed_dict)

			FVE = 1-loss_value/lossbaseline
			duration = time.time() - start_time
			# print progress.
			if step % 50 == 0:
				## Print status
				
				print('Step %d: loss = %.4f; FVE = %5.2f (%.3f sec)' % (step, loss_value, FVE, duration))
      			## save and evaluate the model 
			if (step - 1) % 200 == 0 or (step + 1) == FLAGS.max_steps or step == 1:
				## evaluate and save progress 
				
				steplist.append(step) # list of training steps
				## Evaluate against the training set.
				feed_dict={images_placeholder: data.xtrain, activity_placeholder: data.ytrain, keep_prob_placeholder:1}
				losstrain = sess.run(loss, feed_dict=feed_dict)
				trainlist.append(losstrain) # list of loss on training set
				## Evaluate against the eval set.
				
				feed_dict={images_placeholder: data.xeval, activity_placeholder: data.yeval, keep_prob_placeholder:1}
				losseval = sess.run(loss, feed_dict=feed_dict)

          	
				evallist.append(losseval) # list of loss on eval set
				#computting r eval on eval set
				actpredict_eval = sess.run(model.output, feed_dict=feed_dict)
				reval= np.zeros(data.numcell)
				for icell in range(data.numcell):
					reval[icell], peval = pearsonr(actpredict_eval[:,icell], data.yeval[:,icell])
					if np.isnan(reval[icell]):
						reval[icell] = 0
				rcelllist.append(reval) # list of r eval on eval set
				revalmean = np.mean(reval)
				rmeanlist.append(revalmean) # list of  mean r eval on eval set
				## Evaluate againts early stop data
				feed_dict={images_placeholder: data.xstop, activity_placeholder: data.ystop, keep_prob_placeholder:1}
				lossearlystop = sess.run(loss, feed_dict=feed_dict)
				earlystoplist.append(lossearlystop) # list of loss on early stop set

				## plot and save training
				if FLAGS.savetraining:
					mansavefig(trainlist, earlystoplist, evallist, rmeanlist, steplist, lossbaseline)

				## Finding the minumum loss on the early stop data set
				## check if new early stop min. If so, treat as best trained nextwork 
				if lossearlystop < lossearlystopmin:  # check if early stop is new min 
					lossearlystopmin =  lossearlystop # save loss as new minumum loss on the early stop data set
					FVEeval = 1-losseval/lossbaseline
					lossevalmin=losseval
					print("early stop")
					print("perfornace is")
					print('squared (loss) = %.4f; FVE = %5.3f' % (losseval,FVEeval))
					
					# compute r val again. Could use above values. 
					rval= np.zeros(data.numcell)
					feed_dict={images_placeholder: data.xeval, activity_placeholder: data.yeval, keep_prob_placeholder:1}
					loss_per_nueron_eval = sess.run(loss_per_nueron, feed_dict=feed_dict)
					actpredict_eval = sess.run(model.output, feed_dict=feed_dict)
					rval= np.zeros(data.numcell)
					for icell in range(data.numcell):
						rval[icell], pval = pearsonr(actpredict_eval[:,icell], data.yeval[:,icell])
						if np.isnan(rval[icell]):
							rval[icell] = 0
					rmean = np.mean(rval)
					print('r = %5.3f' % (rmean))
					print("more training!")
					if FLAGS.savenetwork:
						network_save(step) #save the parameters of network
					if FLAGS.save:
						plotandsaver(rval, step, loss_per_nueron_eval, lossbaselinenueron) #save the performance of network 

		print("Final results")
		print("Best perforance ")
		print('r = %5.3f +- %5.3f;  squared (loss) = %.4f;  FVE = %5.3f' % (rmean, np.std(rval)/np.sqrt(data.numcell), losseval, FVEeval))
		print("eval var is") 
		print(np.mean(evalvar)) 
		rerror = np.std(rval)/np.sqrt(data.numcell)