コード例 #1
0
void test_char_rnn(char *cfgfile, char *weightfile, int num, char *seed,
		real_t temp, int rseed, char *token_file) {
	char **tokens = 0;
	if (token_file) {
		size_t n;
		tokens = read_tokens(token_file, &n);
	}

	srand(rseed);
	char *base = basecfg(cfgfile);
	fprintf(stderr, "%s\n", base);

	network *net = load_network(cfgfile, weightfile, 0);
	int inputs = net->inputs;

	int i, j;
	for (i = 0; i < net->n; ++i)
		net->layers[i].temperature = temp;
	int c = 0;
	int len = strlen(seed);
	real_t *input = calloc(inputs, sizeof(real_t));

	/*
	 fill_cpu(inputs, 0, input, 1);
	 for(i = 0; i < 10; ++i){
	 network_predict(net, input);
	 }
	 fill_cpu(inputs, 0, input, 1);
	 */

	for (i = 0; i < len - 1; ++i) {
		c = seed[i];
		input[c] = 1;
		network_predict(net, input);
		input[c] = 0;
		print_symbol(c, tokens);
	}
	if (len)
		c = seed[len - 1];
	print_symbol(c, tokens);
	for (i = 0; i < num; ++i) {
		input[c] = 1;
		real_t *out = network_predict(net, input);
		input[c] = 0;
		for (j = 32; j < 127; ++j) {
			//printf("%d %c %f\n",j, j, out[j]);
		}
		for (j = 0; j < inputs; ++j) {
			if (out[j] < .0001)
				out[j] = 0;
		}
		c = sample_array(out, inputs);
		print_symbol(c, tokens);
	}
	printf("\n");
}
コード例 #2
0
ファイル: go.c プロジェクト: Zumbalamambo/darknetFaceID
int generate_move(network net, int player, float *board, int multi, float thresh, float temp, char *ko, int print)
{
    int i, j;
    for(i = 0; i < net.n; ++i) net.layers[i].temperature = temp;

    float move[361];
    if (player < 0) flip_board(board);
    predict_move(net, board, move, multi);
    if (player < 0) flip_board(board);

    
    for(i = 0; i < 19; ++i){
        for(j = 0; j < 19; ++j){
            if (!legal_go(board, ko, player, i, j)) move[i*19 + j] = 0;
        }
    }

    int indexes[nind];
    top_k(move, 19*19, nind, indexes);
    if(thresh > move[indexes[0]]) thresh = move[indexes[nind-1]];

    for(i = 0; i < 19; ++i){
        for(j = 0; j < 19; ++j){
            if (move[i*19 + j] < thresh) move[i*19 + j] = 0;
        }
    }


    int max = max_index(move, 19*19);
    int row = max / 19;
    int col = max % 19;
    int index = sample_array(move, 19*19);

    if(print){
        top_k(move, 19*19, nind, indexes);
        for(i = 0; i < nind; ++i){
            if (!move[indexes[i]]) indexes[i] = -1;
        }
        print_board(board, player, indexes);
        for(i = 0; i < nind; ++i){
            fprintf(stderr, "%d: %f\n", i+1, move[indexes[i]]);
        }
    }

    if(suicide_go(board, player, row, col)){
        return -1; 
    }
    if(suicide_go(board, player, index/19, index%19)) index = max;
    return index;
}
コード例 #3
0
ファイル: ofxDarknet.cpp プロジェクト: hducg/ofxDarknet
std::string ofxDarknet::rnn(int num, std::string seed, float temp )
{
	int inputs = get_network_input_size( net );

	for( int i = 0; i < net.n; ++i )
	{
		net.layers[ i ].temperature = temp;
	}

	int c = 0;
	int len = seed.length();
	float *input = ( float* ) calloc( inputs, sizeof( float ) );

	std::string sampled_text;

	for( int i = 0; i < len - 1; ++i ) {
		c = seed[ i ];
		input[ c ] = 1;
		network_predict( net, input );
		input[ c ] = 0;
		
		char _c = c;
		sampled_text += _c;
		
	}
	if( len ) c = seed[ len - 1 ];
	
	char _c = c;
	sampled_text += _c;
	
	for( int i = 0; i < num; ++i ) {
		input[ c ] = 1;
		float *out = network_predict( net, input );
		input[ c ] = 0;
		for( int j = 0; j < inputs; ++j ) {
			if( out[ j ] < .0001 ) out[ j ] = 0;
		}
		c = sample_array( out, inputs );
		
		char _c = c;
		sampled_text += _c;
	}

	delete input;

	return sampled_text;
}
コード例 #4
0
void test_tactic_rnn_multi(char *cfgfile, char *weightfile, int num,
		real_t temp, int rseed, char *token_file) {
	char **tokens = 0;
	if (token_file) {
		size_t n;
		tokens = read_tokens(token_file, &n);
	}

	srand(rseed);
	char *base = basecfg(cfgfile);
	fprintf(stderr, "%s\n", base);

	network *net = load_network(cfgfile, weightfile, 0);
	int inputs = net->inputs;

	int i, j;
	for (i = 0; i < net->n; ++i)
		net->layers[i].temperature = temp;
	int c = 0;
	real_t *input = calloc(inputs, sizeof(real_t));
	real_t *out = 0;

	while (1) {
		reset_network_state(net, 0);
		while ((c = getc(stdin)) != EOF && c != 0) {
			input[c] = 1;
			out = network_predict(net, input);
			input[c] = 0;
		}
		for (i = 0; i < num; ++i) {
			for (j = 0; j < inputs; ++j) {
				if (out[j] < .0001)
					out[j] = 0;
			}
			int next = sample_array(out, inputs);
			if (c == '.' && next == '\n')
				break;
			c = next;
			print_symbol(c, tokens);

			input[c] = 1;
			out = network_predict(net, input);
			input[c] = 0;
		}
		printf("\n");
	}
}
コード例 #5
0
ファイル: rnn.c プロジェクト: AlessioTonioni/darknet
void test_tactic_rnn(char *cfgfile, char *weightfile, int num, float temp, int rseed, char *token_file)
{
    char **tokens = 0;
    if(token_file){
        size_t n;
        tokens = read_tokens(token_file, &n);
    }

    srand(rseed);
    char *base = basecfg(cfgfile);
    fprintf(stderr, "%s\n", base);

    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);
    }
    int inputs = get_network_input_size(net);

    int i, j;
    for(i = 0; i < net.n; ++i) net.layers[i].temperature = temp;
    int c = 0;
    float *input = calloc(inputs, sizeof(float));
    float *out = 0;

    while((c = getc(stdin)) != EOF){
        input[c] = 1;
        out = network_predict(net, input);
        input[c] = 0;
    }
    for(i = 0; i < num; ++i){
        for(j = 0; j < inputs; ++j){
            if (out[j] < .0001) out[j] = 0;
        }
        int next = sample_array(out, inputs);
        if(c == '.' && next == '\n') break;
        c = next;
        print_symbol(c, tokens);

        input[c] = 1;
        out = network_predict(net, input);
        input[c] = 0;
    }
    printf("\n");
}