wslのtensorflowでkerasのbackend その12

# 概要
wslのtensorflowでkerasのbackendを試す。
練習問題、やってみた。

# 練習問題
kerasのbackendでxorを学習したウェイトとバイアスを量子化して、fpgaのverilogで再現せよ。

# サンプルコード

```


module xor_(input a, input b, output c);
	reg c;
	reg signed [15:0] x[1:0];
	reg signed [15:0] y;
	reg signed [15:0] h[7:0];
	reg signed [7:0] w10[7:0];
	reg signed [7:0] w11[7:0];
	reg signed [7:0] b1[7:0];
	reg signed [7:0] w2[7:0];
	reg signed [7:0] b2;
	reg signed [7:0] tanh[255:0];
	reg signed [7:0] sigmoid[255:0];
	integer i;
	integer j;
	initial
	begin
		//$display(" a b c y");
		//$monitor(" %b %b %b %d", a, b, c, y);
		y = 0;
		h[0] = 0;
		h[1] = 0;
		h[2] = 0;
		h[3] = 0;
		h[4] = 0;
		h[5] = 0;
		h[6] = 0;
		h[7] = 0;
		w10[0] = -35;
		w10[1] = 3;
		w10[2] = -15;
		w10[3] = 22;
		w10[4] = 23;
		w10[5] = 27;
		w10[6] = -19;
		w10[7] = -12;
		w11[0] = 21;
		w11[1] = 2;
		w11[2] = 20;
		w11[3] = -14;
		w11[4] = -15;
		w11[5] = 19;
		w11[6] = 32;
		w11[7] = 19;
		b1[0] = -7;
		b1[1] = 3;
		b1[2] = 4;
		b1[3] = 3;
		b1[4] = 3;
		b1[5] = -4;
		b1[6] = 4;
		b1[7] = 2;
		w2[0] = 14;
		w2[1] = 7;
		w2[2] = -24;
		w2[3] = -18;
		w2[4] = -25;
		w2[5] = 39;
		w2[6] = -19;
		w2[7] = -14;
		b2 = 9;
		tanh[0] = -10;
		tanh[1] = -10;
		tanh[2] = -10;
		tanh[3] = -10;
		tanh[4] = -10;
		tanh[5] = -10;
		tanh[6] = -10;
		tanh[7] = -10;
		tanh[8] = -10;
		tanh[9] = -10;
		tanh[10] = -10;
		tanh[11] = -10;
		tanh[12] = -10;
		tanh[13] = -10;
		tanh[14] = -10;
		tanh[15] = -10;
		tanh[16] = -10;
		tanh[17] = -10;
		tanh[18] = -10;
		tanh[19] = -10;
		tanh[20] = -10;
		tanh[21] = -10;
		tanh[22] = -10;
		tanh[23] = -10;
		tanh[24] = -10;
		tanh[25] = -10;
		tanh[26] = -10;
		tanh[27] = -10;
		tanh[28] = -10;
		tanh[29] = -10;
		tanh[30] = -10;
		tanh[31] = -10;
		tanh[32] = -10;
		tanh[33] = -10;
		tanh[34] = -10;
		tanh[35] = -10;
		tanh[36] = -10;
		tanh[37] = -10;
		tanh[38] = -10;
		tanh[39] = -10;
		tanh[40] = -10;
		tanh[41] = -10;
		tanh[42] = -10;
		tanh[43] = -10;
		tanh[44] = -10;
		tanh[45] = -10;
		tanh[46] = -10;
		tanh[47] = -10;
		tanh[48] = -10;
		tanh[49] = -10;
		tanh[50] = -10;
		tanh[51] = -10;
		tanh[52] = -10;
		tanh[53] = -10;
		tanh[54] = -10;
		tanh[55] = -10;
		tanh[56] = -10;
		tanh[57] = -10;
		tanh[58] = -10;
		tanh[59] = -10;
		tanh[60] = -10;
		tanh[61] = -10;
		tanh[62] = -10;
		tanh[63] = -10;
		tanh[64] = -10;
		tanh[65] = -10;
		tanh[66] = -10;
		tanh[67] = -10;
		tanh[68] = -10;
		tanh[69] = -10;
		tanh[70] = -10;
		tanh[71] = -10;
		tanh[72] = -10;
		tanh[73] = -10;
		tanh[74] = -10;
		tanh[75] = -10;
		tanh[76] = -10;
		tanh[77] = -10;
		tanh[78] = -10;
		tanh[79] = -10;
		tanh[80] = -10;
		tanh[81] = -10;
		tanh[82] = -10;
		tanh[83] = -10;
		tanh[84] = -10;
		tanh[85] = -10;
		tanh[86] = -10;
		tanh[87] = -10;
		tanh[88] = -10;
		tanh[89] = -10;
		tanh[90] = -10;
		tanh[91] = -10;
		tanh[92] = -10;
		tanh[93] = -10;
		tanh[94] = -10;
		tanh[95] = -10;
		tanh[96] = -10;
		tanh[97] = -10;
		tanh[98] = -10;
		tanh[99] = -10;
		tanh[100] = -10;
		tanh[101] = -10;
		tanh[102] = -10;
		tanh[103] = -10;
		tanh[104] = -10;
		tanh[105] = -10;
		tanh[106] = -10;
		tanh[107] = -10;
		tanh[108] = -10;
		tanh[109] = -10;
		tanh[110] = -10;
		tanh[111] = -9;
		tanh[112] = -9;
		tanh[113] = -9;
		tanh[114] = -9;
		tanh[115] = -8;
		tanh[116] = -8;
		tanh[117] = -7;
		tanh[118] = -7;
		tanh[119] = -6;
		tanh[120] = -5;
		tanh[121] = -4;
		tanh[122] = -3;
		tanh[123] = -2;
		tanh[124] = -1;
		tanh[125] = 0;
		tanh[126] = 0;
		tanh[127] = 1;
		tanh[128] = 2;
		tanh[129] = 3;
		tanh[130] = 4;
		tanh[131] = 5;
		tanh[132] = 6;
		tanh[133] = 6;
		tanh[134] = 7;
		tanh[135] = 7;
		tanh[136] = 8;
		tanh[137] = 8;
		tanh[138] = 8;
		tanh[139] = 8;
		tanh[140] = 9;
		tanh[141] = 9;
		tanh[142] = 9;
		tanh[143] = 9;
		tanh[144] = 9;
		tanh[145] = 9;
		tanh[146] = 9;
		tanh[147] = 9;
		tanh[148] = 9;
		tanh[149] = 9;
		tanh[150] = 9;
		tanh[151] = 9;
		tanh[152] = 9;
		tanh[153] = 9;
		tanh[154] = 9;
		tanh[155] = 9;
		tanh[156] = 9;
		tanh[157] = 9;
		tanh[158] = 9;
		tanh[159] = 9;
		tanh[160] = 9;
		tanh[161] = 9;
		tanh[162] = 9;
		tanh[163] = 9;
		tanh[164] = 9;
		tanh[165] = 9;
		tanh[166] = 9;
		tanh[167] = 9;
		tanh[168] = 9;
		tanh[169] = 9;
		tanh[170] = 9;
		tanh[171] = 9;
		tanh[172] = 9;
		tanh[173] = 9;
		tanh[174] = 9;
		tanh[175] = 9;
		tanh[176] = 9;
		tanh[177] = 9;
		tanh[178] = 9;
		tanh[179] = 9;
		tanh[180] = 9;
		tanh[181] = 9;
		tanh[182] = 9;
		tanh[183] = 9;
		tanh[184] = 9;
		tanh[185] = 9;
		tanh[186] = 9;
		tanh[187] = 9;
		tanh[188] = 9;
		tanh[189] = 9;
		tanh[190] = 9;
		tanh[191] = 9;
		tanh[192] = 9;
		tanh[193] = 9;
		tanh[194] = 9;
		tanh[195] = 9;
		tanh[196] = 9;
		tanh[197] = 9;
		tanh[198] = 9;
		tanh[199] = 9;
		tanh[200] = 9;
		tanh[201] = 9;
		tanh[202] = 9;
		tanh[203] = 9;
		tanh[204] = 9;
		tanh[205] = 9;
		tanh[206] = 9;
		tanh[207] = 9;
		tanh[208] = 9;
		tanh[209] = 9;
		tanh[210] = 9;
		tanh[211] = 9;
		tanh[212] = 9;
		tanh[213] = 9;
		tanh[214] = 9;
		tanh[215] = 9;
		tanh[216] = 9;
		tanh[217] = 9;
		tanh[218] = 9;
		tanh[219] = 9;
		tanh[220] = 9;
		tanh[221] = 9;
		tanh[222] = 9;
		tanh[223] = 9;
		tanh[224] = 9;
		tanh[226] = 9;
		tanh[227] = 9;
		tanh[228] = 9;
		tanh[229] = 9;
		tanh[230] = 9;
		tanh[231] = 9;
		tanh[232] = 9;
		tanh[233] = 9;
		tanh[234] = 9;
		tanh[235] = 9;
		tanh[236] = 9;
		tanh[237] = 9;
		tanh[238] = 9;
		tanh[239] = 9;
		tanh[240] = 9;
		tanh[241] = 9;
		tanh[242] = 9;
		tanh[243] = 9;
		tanh[244] = 9;
		tanh[245] = 9;
		tanh[246] = 9;
		tanh[247] = 9;
		tanh[248] = 9;
		tanh[249] = 9;
		tanh[250] = 9;
		tanh[251] = 9;
		tanh[252] = 9;
		sigmoid[0] = 0;
		sigmoid[1] = 0;
		sigmoid[2] = 0;
		sigmoid[3] = 0;
		sigmoid[4] = 0;
		sigmoid[5] = 0;
		sigmoid[6] = 0;
		sigmoid[7] = 0;
		sigmoid[8] = 0;
		sigmoid[9] = 0;
		sigmoid[10] = 0;
		sigmoid[11] = 0;
		sigmoid[12] = 0;
		sigmoid[13] = 0;
		sigmoid[14] = 0;
		sigmoid[15] = 0;
		sigmoid[16] = 0;
		sigmoid[17] = 0;
		sigmoid[18] = 0;
		sigmoid[19] = 0;
		sigmoid[20] = 0;
		sigmoid[21] = 0;
		sigmoid[22] = 0;
		sigmoid[23] = 0;
		sigmoid[24] = 0;
		sigmoid[25] = 0;
		sigmoid[26] = 0;
		sigmoid[27] = 0;
		sigmoid[28] = 0;
		sigmoid[29] = 0;
		sigmoid[30] = 0;
		sigmoid[31] = 0;
		sigmoid[32] = 0;
		sigmoid[33] = 0;
		sigmoid[34] = 0;
		sigmoid[35] = 0;
		sigmoid[36] = 0;
		sigmoid[37] = 0;
		sigmoid[38] = 0;
		sigmoid[39] = 0;
		sigmoid[40] = 0;
		sigmoid[41] = 0;
		sigmoid[42] = 0;
		sigmoid[43] = 0;
		sigmoid[44] = 0;
		sigmoid[45] = 0;
		sigmoid[46] = 0;
		sigmoid[47] = 0;
		sigmoid[48] = 0;
		sigmoid[49] = 0;
		sigmoid[50] = 0;
		sigmoid[51] = 0;
		sigmoid[52] = 0;
		sigmoid[53] = 0;
		sigmoid[54] = 0;
		sigmoid[55] = 0;
		sigmoid[56] = 0;
		sigmoid[57] = 0;
		sigmoid[58] = 0;
		sigmoid[59] = 0;
		sigmoid[60] = 0;
		sigmoid[61] = 0;
		sigmoid[62] = 0;
		sigmoid[63] = 0;
		sigmoid[64] = 0;
		sigmoid[65] = 0;
		sigmoid[66] = 0;
		sigmoid[67] = 0;
		sigmoid[68] = 0;
		sigmoid[69] = 0;
		sigmoid[70] = 0;
		sigmoid[71] = 0;
		sigmoid[72] = 0;
		sigmoid[73] = 0;
		sigmoid[74] = 0;
		sigmoid[75] = 0;
		sigmoid[76] = 0;
		sigmoid[77] = 0;
		sigmoid[78] = 0;
		sigmoid[79] = 0;
		sigmoid[80] = 0;
		sigmoid[81] = 0;
		sigmoid[82] = 0;
		sigmoid[83] = 0;
		sigmoid[84] = 0;
		sigmoid[85] = 0;
		sigmoid[86] = 0;
		sigmoid[87] = 0;
		sigmoid[88] = 0;
		sigmoid[89] = 0;
		sigmoid[90] = 0;
		sigmoid[91] = 0;
		sigmoid[92] = 0;
		sigmoid[93] = 0;
		sigmoid[94] = 0;
		sigmoid[95] = 0;
		sigmoid[96] = 0;
		sigmoid[97] = 0;
		sigmoid[98] = 0;
		sigmoid[99] = 0;
		sigmoid[100] = 0;
		sigmoid[101] = 0;
		sigmoid[102] = 0;
		sigmoid[103] = 0;
		sigmoid[104] = 1;
		sigmoid[105] = 1;
		sigmoid[106] = 1;
		sigmoid[107] = 1;
		sigmoid[108] = 1;
		sigmoid[109] = 1;
		sigmoid[110] = 1;
		sigmoid[111] = 1;
		sigmoid[112] = 2;
		sigmoid[113] = 2;
		sigmoid[114] = 2;
		sigmoid[115] = 2;
		sigmoid[116] = 2;
		sigmoid[117] = 3;
		sigmoid[118] = 3;
		sigmoid[119] = 3;
		sigmoid[120] = 3;
		sigmoid[121] = 4;
		sigmoid[122] = 4;
		sigmoid[123] = 4;
		sigmoid[124] = 4;
		sigmoid[125] = 5;
		sigmoid[126] = 5;
		sigmoid[127] = 5;
		sigmoid[128] = 5;
		sigmoid[129] = 5;
		sigmoid[130] = 6;
		sigmoid[131] = 6;
		sigmoid[132] = 6;
		sigmoid[133] = 6;
		sigmoid[134] = 7;
		sigmoid[135] = 7;
		sigmoid[136] = 7;
		sigmoid[137] = 7;
		sigmoid[138] = 7;
		sigmoid[139] = 8;
		sigmoid[140] = 8;
		sigmoid[141] = 8;
		sigmoid[142] = 8;
		sigmoid[143] = 8;
		sigmoid[144] = 8;
		sigmoid[145] = 8;
		sigmoid[146] = 8;
		sigmoid[147] = 9;
		sigmoid[148] = 9;
		sigmoid[149] = 9;
		sigmoid[150] = 9;
		sigmoid[151] = 9;
		sigmoid[152] = 9;
		sigmoid[153] = 9;
		sigmoid[154] = 9;
		sigmoid[155] = 9;
		sigmoid[156] = 9;
		sigmoid[157] = 9;
		sigmoid[158] = 9;
		sigmoid[159] = 9;
		sigmoid[160] = 9;
		sigmoid[161] = 9;
		sigmoid[162] = 9;
		sigmoid[163] = 9;
		sigmoid[164] = 9;
		sigmoid[165] = 9;
		sigmoid[166] = 9;
		sigmoid[167] = 9;
		sigmoid[168] = 9;
		sigmoid[169] = 9;
		sigmoid[170] = 9;
		sigmoid[171] = 9;
		sigmoid[172] = 9;
		sigmoid[173] = 9;
		sigmoid[174] = 9;
		sigmoid[175] = 9;
		sigmoid[176] = 9;
		sigmoid[177] = 9;
		sigmoid[178] = 9;
		sigmoid[179] = 9;
		sigmoid[180] = 9;
		sigmoid[181] = 9;
		sigmoid[182] = 9;
		sigmoid[183] = 9;
		sigmoid[184] = 9;
		sigmoid[185] = 9;
		sigmoid[186] = 9;
		sigmoid[187] = 9;
		sigmoid[188] = 9;
		sigmoid[189] = 9;
		sigmoid[190] = 9;
		sigmoid[191] = 9;
		sigmoid[192] = 9;
		sigmoid[193] = 9;
		sigmoid[194] = 9;
		sigmoid[195] = 9;
		sigmoid[196] = 9;
		sigmoid[197] = 9;
		sigmoid[198] = 9;
		sigmoid[199] = 9;
		sigmoid[200] = 9;
		sigmoid[201] = 9;
		sigmoid[202] = 9;
		sigmoid[203] = 9;
		sigmoid[204] = 9;
		sigmoid[205] = 9;
		sigmoid[206] = 9;
		sigmoid[207] = 9;
		sigmoid[208] = 9;
		sigmoid[209] = 9;
		sigmoid[210] = 9;
		sigmoid[211] = 9;
		sigmoid[212] = 9;
		sigmoid[213] = 9;
		sigmoid[214] = 9;
		sigmoid[215] = 9;
		sigmoid[216] = 9;
		sigmoid[217] = 9;
		sigmoid[218] = 9;
		sigmoid[219] = 9;
		sigmoid[220] = 9;
		sigmoid[221] = 9;
		sigmoid[222] = 9;
		sigmoid[223] = 9;
		sigmoid[224] = 9;
		sigmoid[225] = 9;
		sigmoid[226] = 9;
		sigmoid[227] = 9;
		sigmoid[228] = 9;
		sigmoid[229] = 9;
		sigmoid[230] = 9;
		sigmoid[231] = 9;
		sigmoid[232] = 9;
		sigmoid[233] = 9;
		sigmoid[234] = 9;
		sigmoid[235] = 9;
		sigmoid[236] = 9;
		sigmoid[237] = 9;
		sigmoid[238] = 9;
		sigmoid[239] = 9;
		sigmoid[240] = 9;
		sigmoid[241] = 9;
		sigmoid[242] = 9;
		sigmoid[243] = 9;
		sigmoid[244] = 9;
		sigmoid[245] = 9;
		sigmoid[246] = 9;
		sigmoid[247] = 9;
		sigmoid[248] = 9;
		sigmoid[249] = 9;
		sigmoid[250] = 9;
		sigmoid[251] = 9;
	end
	always @(a or b)
	begin
		case ({a, b})
		2'b00:
		begin
			x[0] = 0;
			x[1] = 0;
		end
		2'b01:
		begin
			x[0] = 0;
			x[1] = 1;
		end
		2'b10:
		begin
			x[0] = 1;
			x[1] = 0;
		end
		2'b11:
		begin
			x[0] = 1;
			x[1] = 1;
		end
		endcase
		for (i = 0; i < 8; i = i + 1)
		begin
			h[i] = h[i] + x[0] * w10[i];
		end
		for (i = 0; i < 8; i = i + 1)
		begin
			h[i] = h[i] + x[1] * w11[i];
		end
		for (i = 0; i < 8; i = i + 1)
		begin
			h[i] = h[i] + b1[i];
		end
		for (i = 0; i < 8; i = i + 1)
		begin
			h[i] = h[i] + 126;
			h[i] = tanh[h[i]];
		end
		for (i = 0; i < 8; i = i + 1)
		begin
			y = y + h[i] * w2[i];
		end
		y = y + b2;
		y = y + 126;
		if (y > 250)
		begin
			y = 250;
		end
		if (y < 0)
		begin
			y = 0;
		end
		y = sigmoid[y];
		c <= 0;
		if (y > 1)
		begin
			c <= 1;
		end
	end
endmodule

module testbench;
	reg a,
		b;
	xor_ u(.a(a), .b(b), .c(c));
	initial
	begin
		$display(" a b c");
		$monitor(" %b %b %b", a, b, c);
		a = 0; b = 0; #10;
		a = 0; b = 1; #10;
		a = 1; b = 0; #10;
		a = 1; b = 1; #10;
		$finish;
	end
endmodule

```

# 実行結果

```
>iverilog xor1.v

>vvp a.out
 a b c
 0 0 0
 0 1 1
 1 0 1
 1 1 0
xor1.v:653: $finish called at 40 (1s)
```

以上。