我正在尝试使用using TensorFlow
using Distributions
sess = Session(Graph())
batch_size = 30
num_pixels = 256
###########
# Data base: 10000 arrays, the first array is fill with 1, the second with 2 etc...
arrays_data = zeros(Float32,10000,num_pixels,num_pixels)
arrays_labels = zeros(Float32,10000)
for k in 1:num_pixels, j in 1:num_pixels, i in 1:10000
arrays_data[i,j,k] = i
end
for i in 1:10000
arrays_labels[i] = i
end
###########
# inputs
x = placeholder(Float32, shape= [batch_size, 1, 1, 1])
y = placeholder(Float32)
###########
# Function to create a batch
function create_batch(batch_size)
x = zeros(Float32, batch_size,num_pixels, num_pixels)
y = zeros(Float32, batch_size)
index = shuffle(1:10000) # To choose a random batch
for i in 1:batch_size
x[i, : ,:] = arrays_data[index[i],:,:]
y[i] = arrays_labels[index[i]]
end
y, x
end
###########
# Summary to use TensorBoard
summary = TensorFlow.summary
# Create the different layers ; poids = weight
variable_scope("mymodel" * randstring(), initializer=Normal(0, .001)) do
global poids_1 = get_variable("p1", [3,3,2,1], Float32)
global poids_2 = get_variable("p2",[3,3,3,2],Float32)
global poids_3 = get_variable("p3",[3,3,4,3],Float32)
global poids_4 = get_variable("p4",[3,3,4,4],Float32)
global poids_5 = get_variable("p5",[3,3,4,4],Float32)
global poids_6 = get_variable("p6",[3,3,4,4],Float32)
global poids_7 = get_variable("p7",[3,3,8,4],Float32)
global poids_8 = get_variable("p8",[3,3,8,8],Float32)
global biases_1 = get_variable("b1",[2],Float32)
global biases_2 = get_variable("b2",[3],Float32)
global biases_3 = get_variable("b3",[4],Float32)
global biases_4 = get_variable("b4",[4],Float32)
global biases_5 = get_variable("b5",[4],Float32)
global biases_6 = get_variable("b6",[4],Float32)
global biases_7 = get_variable("b7",[8],Float32)
global biases_8 = get_variable("b8",[8],Float32)
end
logits_1 = nn.relu(nn.conv2d_transpose(x, poids_1, [batch_size,2,2,2], [1,2,2,1],padding = "SAME") + biases_1)
logits_2 = nn.relu(nn.conv2d_transpose(logits_1,poids_2, [batch_size,4,4,3], [1,2,2,1],padding = "SAME") + biases_2)
logits_3 = nn.relu(nn.conv2d_transpose(logits_2,poids_3, [batch_size,8,8,4], [1,2,2,1],padding = "SAME") + biases_3)
logits_4 = nn.relu(nn.conv2d_transpose(logits_3,poids_4, [batch_size,16,16,4], [1,2,2,1],padding = "SAME") + biases_4)
logits_5 = nn.relu(nn.conv2d_transpose(logits_4,poids_5, [batch_size,32,32,4], [1,2,2,1],padding = "SAME") + biases_5)
logits_6 = nn.relu(nn.conv2d_transpose(logits_5,poids_6, [batch_size,64,64,4], [1,2,2,1],padding = "SAME") + biases_6)
logits_7 = nn.relu(nn.conv2d_transpose(logits_6,poids_7, [batch_size,128,128,8], [1,2,2,1],padding = "SAME") + biases_7)
logits_8 = nn.relu(nn.conv2d_transpose(logits_7,poids_8, [batch_size,256,256,8], [1,2,2,1],padding = "SAME") + biases_8)
logits_8 = reduce_sum(logits_8, axis=[4])
logits = reshape(logits_8, [batch_size,num_pixels*num_pixels]) # Output of network
# Don't use a softmax here...
least_square = reduce_mean(sqrt(sum((y - logits).^2))) # Loss function
optimizer = train.AdamOptimizer(0.0001)
train_op = train.minimize(optimizer,least_square)
error = sqrt(sum((y - logits).^2)./(num_pixels.*num_pixels.*batch_size))
summary.histogram("Error",error)
merged = summary.merge_all()
run(sess, global_variables_initializer())
# summary_writer = summary.FileWriter("Folder Path") # If you want use TensorBoard
# Train loop
for i in 1:1500
batch = create_batch(batch_size)
x_ = run(sess, train_op, Dict(x => reshape(batch[1], (batch_size,1,1,1)), y => reshape(batch[2], (batch_size,num_pixels*num_pixels))))
if i%100 == 1
err = run(sess, error, Dict(x => reshape(batch[1], (batch_size,1,1,1)), y => reshape(batch[2], (batch_size,num_pixels*num_pixels))))
info("train $i , error = $err")
end
# If you use TensorBoard, please use the following commands
# newer = run(sess,merged, Dict(x => reshape(batch[1], (batch_size,1,1,1)), y => reshape(batch[2], (batch_size,num_pixels*num_pixels))))
# write(summary_writer, newer, i)
end
close(sess)
使用左右键向前和向后移动此游戏中的角色,但我似乎无法移动角色。我没有运气地看着这些答案:
How to use keyListener properly in Java和 How do i use KeyListener in java?
这是我的代码:
KeyListner
有谁能告诉我我在哪里以及我做错了什么?
答案 0 :(得分:1)
当你这样做时
Design A2 = new Design() {
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
您正在覆盖您在Design
抽象类中编写的代码而只抛出异常。
您的Design
课程不一定是abstract
,因为它没有未实现的方法。所以我可以看到两种方法来使用你在Design
类中编写的代码:
第一种方式
删除abstract
关键字,以便您可以使用Design
关键字将new
类实例化为普通类。所以以这种方式声明Design
类:
class Design extends JPanel implements ActionListener, KeyListener
然后以这种方式实例化A2:
Design A2 = new Design();
第二种方式
如果出于某种原因,你需要将类作为抽象来以这种方式实例化你的A2对象:
Design A2 = new Design() {};
这将有效,而不会覆盖您在Design
课程中编写的任何方法。但这没有多大意义,上面的第一种方式更有意义。
注1
现在,如果您在Design.keyPressed
中添加打印输出,则可以看到它被调用(您将在控制台中看到日志):
public void keyPressed(KeyEvent e) {
System.out.println("KeyEvent: "+e);
int key = e.getKeyCode();
...
注2
同时查看keyPressed
方法,我认为您还需要更新字符X轴坐标,因此在if语句后面添加以下行:
xAxisOfCharacter += velocityOfCharacter;
它应该是这样的:
public void keyPressed(KeyEvent e) {
System.out.println("KeyEvent: "+e);
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT) {
velocityOfCharacter = -5;
}
else if(key == KeyEvent.VK_RIGHT) {
velocityOfCharacter = 5;
}
xAxisOfCharacter += velocityOfCharacter;
}