我有一个日期如下的json文件:
def neural_network(num_hid, epochs,
learning_rate, X, Y):
#num_hid - number of neurons in the hidden layer
#X - dataX - shape (10000, 784)
#Y - labels - shape (10000, 10)
#inicialization
W1 = np.random.randn(784, num_hid) * 0.01
W2 = np.random.randn(num_hid, 10) * 0.01
b1 = np.zeros((1, num_hid))
b2 = np.zeros((1, 10))
correct = 0
for x in range(1, epochs+1):
#feedforward
Z1 = np.dot(X, W1) + b1
A1 = tanh(Z1)
Z2 = np.dot(A1, W2) + b2
A2 = softmax(Z2)
###############################################################################START
m = X.shape[1] #-> 784
loss = - np.sum((Y * np.log(A2)), axis=0, keepdims=True)
cost = np.sum(loss, axis=1) / m
#backpropagation
dZ2 = A2 - Y
dW2 = (1/m)*np.dot(A1.T, dZ2)
db2 = (1/m)*np.sum(dZ2, axis = 1, keepdims = True)
dZ1 = np.multiply(np.dot(dZ2, W2.T), 1 - np.power(A1, 2))
dW1 = (1/m)*np.dot(X.T, dZ1)
db1 = (1/m)*np.sum(dZ1, axis = 1, keepdims = True)
###############################################################################STOP
#parameters update - gradient descent
W1 = W1 - dW1*learning_rate
b1 = b1 - db1*learning_rate
W2 = W2 - dW2*learning_rate
b2 = b2 - db2*learning_rate
correct = 0
for i in range(np.shape(Y)[0]):
guess = np.argmax(A2[i, :])
ans = np.argmax(Y[i, :])
# print(str(x) + " " + str(i) + ". " +"guess: ", guess, "| ans: ", ans)
if guess == ans:
correct = correct + 1
# correct = np.sum(np.argmax(A2,axis=1) == np.argmax(Y,axis=1))
# print(correct)
accuracy = (correct/np.shape(Y)[0]) * 100
print(accuracy)
我在课堂上使用
{
...
"dob":"20001010",
"registerdate":"20001010121212",
...
}
然后在我的测试中,我使用Gson转换文件
public class User implements Serializable {
... some other
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd
public java.util.Date dob; // this works
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
public java.util.Date registerdate;
}
但是当运行时,我得到:
User user = new Gson().fromJson("user.json", User.class);
我害怕:yyyyMMddHHmmss或YYYYMMddHHmmss或YYYYMMDDHHmmss相同的结果
pom:
com.google.gson.JsonSyntaxException: 20001010121212
答案 0 :(得分:1)
如果您使用的是GSON,则需要解析日期,例如
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GSONExample {
private static String json = "{\r\n" + " \"dob\":\"20001010121212\"\r\n" + "}";
public static void main(String[] args) {
Gson gson = new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create();
ItemDob itemDob = gson.fromJson(json, ItemDob.class);
System.out.println(itemDob.getDob());
}
}
class ItemDob {
private Date dob;
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
输出
IST 2000年10月10日星期二12:12:12
编辑 如果要使用注释指定日期格式,请使用jackson。例如,
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
private static String json = "{\r\n" +
" \"dob\": \"20001010121212\"\r\n" +
"}";
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
ItemDob itemDob = new ObjectMapper().readValue(json, ItemDob.class);
System.out.println(itemDob.getDob());
}
}
class ItemDob {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
private Date dob;
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
输出:
IST 2000年10月10日17:42:12
答案 1 :(得分:1)
基本上 com.google.gson.Gson 无法识别注释 com.fasterxml.jackson.annotation.JsonFormat (两个不同的库)
这是仅使用来自fasterxml lib的ObjectMapper的另一种解决方案
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
import java.util.Date;
public class DemoApplication {
public static void main(String[] args) throws JsonProcessingException {
String json = "{\"dob\":\"20001010121212\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println(user.dob);
}
}
class User implements Serializable {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
public Date dob;
}